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