src/Tools/isac/BridgeLibisabelle/mathml.sml
author wneuper <Walther.Neuper@jku.at>
Wed, 19 Oct 2022 10:43:04 +0200
changeset 60567 bb3140a02f3d
parent 60258 a5eed208b22f
child 60673 ef24b1eed505
permissions -rw-r--r--
eliminate term2str in src, Prog_Tac.*_adapt_to_type
neuper@37906
     1
(* translate formulae from Isabelle-string format to xml-format.
neuper@37906
     2
   author: Walther Neuper 030701
neuper@37906
     3
   (c) isac-team 2003
walther@60258
     4
walther@60258
     5
This code remains in order to
walther@60258
     6
keep tests of interaction Java-frontend <-> Kernel running.
neuper@37906
     7
*)
neuper@37906
     8
walther@60258
     9
val indentation = 2;
walther@59905
    10
walther@60244
    11
(*
neuper@37906
    12
   EXCEPT xml-coding issues (2).
neuper@37906
    13
   (2) have a _reverse_ method 
neuper@37906
    14
   'isac.util.parser.FormalizationDigest.decodeEntities' 
neuper@37906
    15
   called within Formula#toSMLString in java
neuper@37906
    16
neuper@37906
    17
   ad(2) decode "<" ---> "&lt;", decode ">" ---> "&gt;"
neuper@37906
    18
         decode "&" ---> "&amp;"
wneuper@59260
    19
   called for term2xml; + see "fun encode" below*)
walther@59865
    20
fun decode (str: TermC.as_string) = 
neuper@37906
    21
    let fun dec [] = []
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
walther@60258
    44
val i = indentation;
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"
walther@60258
    48
  | xmlstr i (XML.Elem (("TERM", []), [_])) = 
wneuper@59224
    49
    indent i ^ "(" ^ "TERM" ^ ")" ^ "\n" ^
walther@60257
    50
    indent (i + 1) ^ "rm libisabelle: xt NOT DECODED" ^ "\n" ^
wneuper@59224
    51
    indent i ^ "(/" ^ "TERM" ^ ")" ^ "\n"
wneuper@59154
    52
  | xmlstr i (XML.Elem ((str, []), trees)) = 
wneuper@59163
    53
    indent i ^ "(" ^ str ^ ")" ^ "\n" ^
wneuper@59154
    54
      List.foldr op ^ "" (map (xmlstr (i + 1)) trees) ^
wneuper@59163
    55
    indent i ^ "(/" ^ str ^ ")" ^ "\n"
wneuper@59158
    56
  | xmlstr i (XML.Elem ((str, [(flag, value)]), trees)) = 
wneuper@59163
    57
    indent i ^ "(" ^ str ^ " " ^ flag ^ "=" ^ value  ^ ")" ^ "\n" ^
wneuper@59154
    58
      List.foldr op ^ "" (map (xmlstr (i + 1)) trees) ^
wneuper@59163
    59
    indent i ^ "(/" ^ str ^ ")" ^ "\n"
wneuper@59154
    60
  | xmlstr _ (XML.Elem ((_, (_ :: _)), _)) = 
walther@59962
    61
    raise ERROR "xmlstr: TODO review attribute \"status\" etc";
neuper@37906
    62
neuper@37906
    63
fun strs2xml strs = foldl (op ^) ("", strs); 
neuper@37906
    64
neuper@37947
    65
(*WN071016 checked that _all_ Frontend/interface.sml uses this*)
neuper@37906
    66
fun term2xml j t = 
neuper@37906
    67
    indt (j+i) ^ "<MATHML>\n" ^ 
walther@59868
    68
    indt (j+2*i) ^ "<ISA> " ^ (decode o UnparseC.term) t ^ " </ISA>\n" ^
neuper@37906
    69
    indt (j+i) ^ "</MATHML>";
Walther@60567
    70
(*val t = TermC.parse_test @{context} "equality e_";
neuper@38031
    71
  writeln (term2xml 8 t);
neuper@37906
    72
          <MATHML>
neuper@37906
    73
            <ISA> equality e_ </ISA>
neuper@37906
    74
          <MATHML> *)
wneuper@59127
    75
fun xml_of_term t =
wneuper@59127
    76
  XML.Elem (("MATHML", []),
walther@59868
    77
    [XML.Elem (("ISA", []), [XML.Text ((decode o UnparseC.term) t)])])
wneuper@59127
    78
fun xml_of_terms ts = map xml_of_term ts
wneuper@59211
    79
wneuper@59211
    80
(* intermediate replacements while introducing transfer of terms by libisabelle *)
wneuper@59458
    81
fun xml_of_term_NEW (t : term) =
wneuper@59222
    82
  XML.Elem (("FORMULA", []), [
walther@59868
    83
    XML.Elem (("ISA", []), [XML.Text ((decode o UnparseC.term) t)]),
walther@59868
    84
    XML.Text ("(TERM)\n " ^ UnparseC.term t  ^ "\n(/TERM)")
walther@59616
    85
    ])
walther@59616
    86
neuper@37906
    87
(*version for TextIO*)                                                         
walther@60258
    88
fun terms2xml _ [] = ""
neuper@37906
    89
  | terms2xml j (t::ts) = term2xml j t ^ terms2xml j ts;
neuper@38031
    90
(*version for writeln: extra \n*)
walther@60258
    91
fun terms2xml' _ [] = ""
neuper@37906
    92
  | terms2xml' j [t] = term2xml j t
neuper@37906
    93
  | terms2xml' j (t::ts) = term2xml j t ^"\n"^ terms2xml' j ts;
neuper@37906
    94
   
neuper@37906
    95
(*WN060513 'cterm' means the Isabelle-type*)
neuper@37906
    96
fun cterm2xml j ct = 
neuper@37906
    97
    indt (j+i) ^ "<MATHML>\n" ^ 
neuper@37906
    98
    indt (j+2*i) ^ "<ISA> " ^ ct ^ " </ISA>\n" ^
neuper@37906
    99
    indt (j+i) ^ "</MATHML>\n";
wneuper@59127
   100
fun xml_of_cterm ct = 
wneuper@59127
   101
  XML.Elem (("MATHML", []),
wneuper@59127
   102
    [XML.Elem (("ISA", []), [XML.Text ct])])
wneuper@59127
   103
neuper@37906
   104
(*version for TextIO*)                                                         
walther@60258
   105
fun cterms2xml _ [] = ""
neuper@37906
   106
  | cterms2xml j (t::ts) = cterm2xml j t ^ cterms2xml j ts;
neuper@38031
   107
(*version for writeln: extra \n*)
walther@60258
   108
fun cterms2xml' _ [] = ""
neuper@37906
   109
  | cterms2xml' j (t::ts) = cterm2xml j t ^"\n"^ cterms2xml j ts;