src/Tools/isac/BridgeLibisabelle/mathml.sml
author Walther Neuper <walther.neuper@jku.at>
Wed, 22 Apr 2020 16:53:03 +0200
changeset 59905 5e9118030ed9
parent 59868 d77aa0992e0f
child 59962 6a59d252345d
permissions -rw-r--r--
cleanup Celem
neuper@37906
     1
(* translate formulae from Isabelle-string format to xml-format.
neuper@37906
     2
   TODO implement MathML
neuper@37906
     3
   author: Walther Neuper 030701
neuper@37906
     4
   (c) isac-team 2003
neuper@37906
     5
*)
neuper@37906
     6
walther@59905
     7
type xml = string; (* rm together with old code replaced by XML.tree *)
walther@59905
     8
neuper@37906
     9
(*.decode Isabelle-strings to the format as seen by the user (1)
neuper@37906
    10
   EXCEPT xml-coding issues (2).
neuper@37906
    11
   (2) have a _reverse_ method 
neuper@37906
    12
   'isac.util.parser.FormalizationDigest.decodeEntities' 
neuper@37906
    13
   called within Formula#toSMLString in java
neuper@37906
    14
neuper@37947
    15
   ad(1) decode "^^^" ---> "^"; see Knowledge/Atools.thy;
neuper@37906
    16
   ad(2) decode "<" ---> "&lt;", decode ">" ---> "&gt;"
neuper@37906
    17
         decode "&" ---> "&amp;"
wneuper@59260
    18
   called for term2xml; + see "fun encode" below*)
walther@59865
    19
fun decode (str: TermC.as_string) = 
neuper@37906
    20
    let fun dec [] = []
neuper@37906
    21
	  | dec ("^"::"^"::"^"::cs) = "^"::(dec cs)
neuper@37906
    22
	  | dec ("&"::cs) = "&"::"a"::"m"::"p"::";"::(dec cs)
neuper@37906
    23
	  | dec ("<"::cs) = "&"::"l"::"t"::";"::(dec cs)
neuper@37906
    24
	  | dec (">"::cs) = "&"::"g"::"t"::";"::(dec cs)
neuper@37906
    25
	  | dec (c::cs) = c::(dec cs)
walther@59865
    26
    in (implode o dec o Symbol.explode) str: TermC.as_string end;
wneuper@59177
    27
wneuper@59177
    28
fun dop_leading _ [] = []
wneuper@59177
    29
  | dop_leading c (c' :: cs) =
wneuper@59177
    30
    if c = c' then dop_leading c cs else (c' :: cs)
wneuper@59177
    31
fun rm_doublets _ singled [] = singled
wneuper@59177
    32
  | rm_doublets c singled (c' :: cs) =
wneuper@59177
    33
    if c = c'
wneuper@59177
    34
    then 
wneuper@59177
    35
      let val cs' = dop_leading "^" cs
wneuper@59177
    36
      in rm_doublets c (singled @ [c']) cs' end
wneuper@59177
    37
    else rm_doublets c (singled @ [c']) cs
walther@59865
    38
fun encode (str : TermC.as_string) =
wneuper@59154
    39
    let fun enc [] = []
wneuper@59154
    40
	  | enc ("^" :: cs) = "^" :: "^" :: "^" :: (enc cs)
wneuper@59154
    41
	  | enc (c :: cs) = c :: (enc cs)
wneuper@59177
    42
    in str |> Symbol.explode |> rm_doublets "^" [] |>  enc |>  implode end;
neuper@37906
    43
wneuper@59154
    44
val indentation = 2;
wneuper@59154
    45
wneuper@59224
    46
(* proper <> is translated to html; however, () creates readable output.. *)
wneuper@59154
    47
fun xmlstr i (XML.Text str) = indent i ^ str ^ "\n"
wneuper@59224
    48
  | xmlstr i (XML.Elem (("TERM", []), [xt])) = 
wneuper@59224
    49
    indent i ^ "(" ^ "TERM" ^ ")" ^ "\n" ^
walther@59616
    50
    indent (i + 1) ^
walther@59616
    51
(*//---------------------------------- rm libisabelle ---------------------------------------\\* )
walther@59868
    52
      (xt |> Codec.decode Codec.term |> Codec.the_success |> UnparseC.term)
walther@59616
    53
( *------------------------------------ rm libisabelle -----------------------------------------*)
walther@59616
    54
      "rm libisabelle: xt NOT DECODED"
walther@59616
    55
(*\\---------------------------------- rm libisabelle ---------------------------------------//*)
walther@59616
    56
      ^ "\n" ^
wneuper@59224
    57
    indent i ^ "(/" ^ "TERM" ^ ")" ^ "\n"
wneuper@59154
    58
  | xmlstr i (XML.Elem ((str, []), trees)) = 
wneuper@59163
    59
    indent i ^ "(" ^ str ^ ")" ^ "\n" ^
wneuper@59154
    60
      List.foldr op ^ "" (map (xmlstr (i + 1)) trees) ^
wneuper@59163
    61
    indent i ^ "(/" ^ str ^ ")" ^ "\n"
wneuper@59158
    62
  | xmlstr i (XML.Elem ((str, [(flag, value)]), trees)) = 
wneuper@59163
    63
    indent i ^ "(" ^ str ^ " " ^ flag ^ "=" ^ value  ^ ")" ^ "\n" ^
wneuper@59154
    64
      List.foldr op ^ "" (map (xmlstr (i + 1)) trees) ^
wneuper@59163
    65
    indent i ^ "(/" ^ str ^ ")" ^ "\n"
wneuper@59154
    66
  | xmlstr _ (XML.Elem ((_, (_ :: _)), _)) = 
wneuper@59154
    67
    error "xmlstr: TODO review attribute \"status\" etc";
neuper@37906
    68
neuper@37906
    69
fun strs2xml strs = foldl (op ^) ("", strs); 
neuper@38031
    70
(* writeln (strs2xml ["<XXX> xxx </XXX>\n","<YYY> yyy </YYY>\n"]);
neuper@37906
    71
<XXX> xxx </XXX>
neuper@37906
    72
<YYY> yyy </YYY>*)
neuper@37906
    73
neuper@37906
    74
val indentation = 2;
neuper@37906
    75
val i = indentation;
neuper@37906
    76
neuper@37947
    77
(*WN071016 checked that _all_ Frontend/interface.sml uses this*)
neuper@37906
    78
fun term2xml j t = 
neuper@37906
    79
    indt (j+i) ^ "<MATHML>\n" ^ 
walther@59868
    80
    indt (j+2*i) ^ "<ISA> " ^ (decode o UnparseC.term) t ^ " </ISA>\n" ^
neuper@37906
    81
    indt (j+i) ^ "</MATHML>";
neuper@37906
    82
(*val t = str2term "equality e_";
neuper@38031
    83
  writeln (term2xml 8 t);
neuper@37906
    84
          <MATHML>
neuper@37906
    85
            <ISA> equality e_ </ISA>
neuper@37906
    86
          <MATHML> *)
wneuper@59127
    87
fun xml_of_term t =
wneuper@59127
    88
  XML.Elem (("MATHML", []),
walther@59868
    89
    [XML.Elem (("ISA", []), [XML.Text ((decode o UnparseC.term) t)])])
wneuper@59127
    90
fun xml_of_terms ts = map xml_of_term ts
wneuper@59154
    91
fun xml_to_term 
wneuper@59221
    92
    ((XML.Elem (("MATHML", []), [
wneuper@59389
    93
        XML.Elem (("ISA", []), [XML.Text str])]))) = str |> encode |> TermC.str2term
wneuper@59154
    94
  | xml_to_term xx = raise ERROR ("xml_to_term wrong arg: " ^ xmlstr 0 xx)
wneuper@59221
    95
fun xml_to_term_NEW 
wneuper@59221
    96
    ((XML.Elem (("FORMULA", []), [
wneuper@59389
    97
        XML.Elem (("ISA", []), [XML.Text str])]))) = str |> encode |> TermC.str2term
wneuper@59221
    98
  | xml_to_term_NEW xx = raise ERROR ("xml_to_term_NEW wrong arg: " ^ xmlstr 0 xx)
wneuper@59154
    99
fun xml_to_terms ts = map xml_to_term ts
wneuper@59221
   100
fun xml_to_terms_NEW ts = map xml_to_term_NEW ts
wneuper@59211
   101
wneuper@59211
   102
(* intermediate replacements while introducing transfer of terms by libisabelle *)
wneuper@59458
   103
fun xml_of_term_NEW (t : term) =
wneuper@59222
   104
  XML.Elem (("FORMULA", []), [
walther@59868
   105
    XML.Elem (("ISA", []), [XML.Text ((decode o UnparseC.term) t)]),
walther@59616
   106
(*//---------------------------------- rm libisabelle ---------------------------------------\\* )
walther@59616
   107
    XML.Elem (("TERM", []), [Codec.encode Codec.term t])
walther@59616
   108
( *------------------------------------ rm libisabelle -----------------------------------------*)
walther@59868
   109
    XML.Text ("(TERM)\n " ^ UnparseC.term t  ^ "\n(/TERM)")
walther@59616
   110
(*\\---------------------------------- rm libisabelle ---------------------------------------//*)
walther@59616
   111
    ])
walther@59616
   112
wneuper@59221
   113
(* unused: formulas come as strings from frontend and are parsed by Isabelle *)
wneuper@59221
   114
fun xml_to_term_UNUSED
wneuper@59221
   115
  ((XML.Elem (("FORMULA", []), [
wneuper@59211
   116
      XML.Elem (("ISA", []), [XML.Text _]),
walther@59616
   117
    XML.Elem (("TERM", []), [xt])]))) =
walther@59616
   118
(*//---------------------------------- rm libisabelle ---------------------------------------\\* )
walther@59616
   119
      xt |> Codec.decode Codec.term |> Codec.the_success
walther@59616
   120
( *------------------------------------ rm libisabelle -----------------------------------------*)
walther@59616
   121
      Const ("rm libisabelle: xt NOT DECODED", HOLogic.realT)
walther@59616
   122
(*\\---------------------------------- rm libisabelle ---------------------------------------//*)
wneuper@59221
   123
  | xml_to_term_UNUSED xx = raise ERROR ("xml_to_term_NEW wrong arg: " ^ xmlstr 0 xx)
neuper@37906
   124
neuper@37906
   125
(*version for TextIO*)                                                         
neuper@37906
   126
fun terms2xml j [] = ""
neuper@37906
   127
  | terms2xml j (t::ts) = term2xml j t ^ terms2xml j ts;
neuper@38031
   128
(*version for writeln: extra \n*)
neuper@37906
   129
fun terms2xml' j [] = ""
neuper@37906
   130
  | terms2xml' j [t] = term2xml j t
neuper@37906
   131
  | terms2xml' j (t::ts) = term2xml j t ^"\n"^ terms2xml' j ts;
neuper@37906
   132
   
neuper@37906
   133
(*WN060513 'cterm' means the Isabelle-type*)
neuper@37906
   134
fun cterm2xml j ct = 
neuper@37906
   135
    indt (j+i) ^ "<MATHML>\n" ^ 
neuper@37906
   136
    indt (j+2*i) ^ "<ISA> " ^ ct ^ " </ISA>\n" ^
neuper@37906
   137
    indt (j+i) ^ "</MATHML>\n";
wneuper@59127
   138
fun xml_of_cterm ct = 
wneuper@59127
   139
  XML.Elem (("MATHML", []),
wneuper@59127
   140
    [XML.Elem (("ISA", []), [XML.Text ct])])
wneuper@59155
   141
fun xml_to_cterm
wneuper@59155
   142
    (XML.Elem (("MATHML", []),
wneuper@59155
   143
      [XML.Elem (("ISA", []), [XML.Text ct])])) = ct
wneuper@59155
   144
  | xml_to_cterm x = raise ERROR ("xml_to_cterm wrong arg: " ^ xmlstr 0 x)
wneuper@59127
   145
neuper@37906
   146
(*version for TextIO*)                                                         
neuper@37906
   147
fun cterms2xml j [] = ""
neuper@37906
   148
  | cterms2xml j (t::ts) = cterm2xml j t ^ cterms2xml j ts;
neuper@38031
   149
(*version for writeln: extra \n*)
neuper@37906
   150
fun cterms2xml' j [] = ""
neuper@37906
   151
  | cterms2xml' j (t::ts) = cterm2xml j t ^"\n"^ cterms2xml j ts;
neuper@37906
   152
neuper@38031
   153
(* writeln(cterms2xml 5 ["cterm1", "cterm2"]);
neuper@37906
   154
       <MATHML>
neuper@37906
   155
         <ISA> cterm1 </ISA>
neuper@37906
   156
       </MATHML>
neuper@37906
   157
       <MATHML>
neuper@37906
   158
         <ISA> cterm2 </ISA>
neuper@37906
   159
       </MATHML>
neuper@37906
   160
*)