src/HOL/Import/xmlconv.ML
author haftmann
Fri, 11 Jun 2010 17:14:02 +0200
changeset 37407 61dd8c145da7
parent 35130 0991c84e8dcf
permissions -rw-r--r--
declare lex_prod_def [code del]
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     1
signature XML_CONV =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     2
sig
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     3
  type 'a input = XML.tree -> 'a
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     4
  type 'a output = 'a -> XML.tree
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     5
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     6
  exception ParseException of string
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     7
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     8
  val xml_of_typ: typ output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
     9
  val typ_of_xml: typ input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    10
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    11
  val xml_of_term: term output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    12
  val term_of_xml : term input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    13
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    14
  val xml_of_mixfix: mixfix output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    15
  val mixfix_of_xml: mixfix input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    16
  
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    17
  val xml_of_proof: Proofterm.proof output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    18
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    19
  val xml_of_bool: bool output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    20
  val bool_of_xml: bool input
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
    21
                  
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    22
  val xml_of_int: int output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    23
  val int_of_xml: int input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    24
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    25
  val xml_of_string: string output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    26
  val string_of_xml: string input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    27
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    28
  val xml_of_list: 'a output -> 'a list output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    29
  val list_of_xml: 'a input -> 'a list input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    30
  
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    31
  val xml_of_tuple : 'a output -> 'a list output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    32
  val tuple_of_xml: 'a input -> int -> 'a list input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    33
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    34
  val xml_of_option: 'a output -> 'a option output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    35
  val option_of_xml: 'a input -> 'a option input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    36
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    37
  val xml_of_pair: 'a output -> 'b output -> ('a * 'b) output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    38
  val pair_of_xml: 'a input -> 'b input -> ('a * 'b) input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    39
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    40
  val xml_of_triple: 'a output -> 'b output -> 'c output -> ('a * 'b * 'c) output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    41
  val triple_of_xml: 'a input -> 'b input -> 'c input -> ('a * 'b * 'c) input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    42
  
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    43
  val xml_of_unit: unit output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    44
  val unit_of_xml: unit input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    45
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    46
  val xml_of_quadruple: 'a output -> 'b output -> 'c output -> 'd output -> ('a * 'b * 'c * 'd) output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    47
  val quadruple_of_xml: 'a input -> 'b input -> 'c input -> 'd input -> ('a * 'b * 'c * 'd) input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    48
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    49
  val xml_of_quintuple: 'a output -> 'b output -> 'c output -> 'd output -> 'e output -> ('a * 'b * 'c * 'd * 'e) output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    50
  val quintuple_of_xml: 'a input -> 'b input -> 'c input -> 'd input -> 'e input -> ('a * 'b * 'c * 'd * 'e) input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    51
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    52
  val wrap : string -> 'a output -> 'a output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    53
  val unwrap : ('a -> 'b) -> 'a input -> 'b input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    54
  val wrap_empty : string output
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    55
  val unwrap_empty : 'a -> 'a input
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    56
  val name_of_wrap : XML.tree -> string
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    57
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    58
  val write_to_file: 'a output -> string -> 'a -> unit
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    59
  val read_from_file: 'a input -> string -> 'a
19089
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
    60
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
    61
  val serialize_to_file : 'a output -> string -> 'a -> unit
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
    62
  val deserialize_from_file : 'a input -> string -> 'a
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    63
end;
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    64
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    65
structure XMLConv : XML_CONV =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    66
struct
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    67
  
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    68
type 'a input = XML.tree -> 'a
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    69
type 'a output = 'a -> XML.tree
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    70
exception ParseException of string
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    71
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    72
fun parse_err s = raise ParseException s
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    73
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    74
fun xml_of_class name = XML.Elem ("class", [("name", name)], []);
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    75
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    76
fun class_of_xml (XML.Elem ("class", [("name", name)], [])) = name
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    77
  | class_of_xml _ = parse_err "class"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    78
 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    79
fun xml_of_typ (TVar ((s, i), S)) = XML.Elem ("TVar",
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    80
      ("name", s) :: (if i=0 then [] else [("index", string_of_int i)]),
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    81
      map xml_of_class S)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    82
  | xml_of_typ (TFree (s, S)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    83
      XML.Elem ("TFree", [("name", s)], map xml_of_class S)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    84
  | xml_of_typ (Type (s, Ts)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    85
      XML.Elem ("Type", [("name", s)], map xml_of_typ Ts);
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    86
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    87
fun intofstr s i = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    88
    case Int.fromString i of 
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
    89
        NONE => parse_err (s^", invalid index: "^i)
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    90
      | SOME i => i
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    91
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    92
fun typ_of_xml (XML.Elem ("TVar", [("name", s)], cs)) = TVar ((s,0), map class_of_xml cs)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    93
  | typ_of_xml (XML.Elem ("TVar", [("name", s), ("index", i)], cs)) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    94
    TVar ((s, intofstr "TVar" i), map class_of_xml cs)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    95
  | typ_of_xml (XML.Elem ("TFree", [("name", s)], cs)) = TFree (s, map class_of_xml cs)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    96
  | typ_of_xml (XML.Elem ("Type", [("name", s)], ts)) = Type (s, map typ_of_xml ts)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    97
  | typ_of_xml _ = parse_err "type"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    98
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
    99
fun xml_of_term (Bound i) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   100
      XML.Elem ("Bound", [("index", string_of_int i)], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   101
  | xml_of_term (Free (s, T)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   102
      XML.Elem ("Free", [("name", s)], [xml_of_typ T])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   103
  | xml_of_term (Var ((s, i), T)) = XML.Elem ("Var",
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   104
      ("name", s) :: (if i=0 then [] else [("index", string_of_int i)]),
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   105
      [xml_of_typ T])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   106
  | xml_of_term (Const (s, T)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   107
      XML.Elem ("Const", [("name", s)], [xml_of_typ T])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   108
  | xml_of_term (t $ u) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   109
      XML.Elem ("App", [], [xml_of_term t, xml_of_term u])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   110
  | xml_of_term (Abs (s, T, t)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   111
      XML.Elem ("Abs", [("vname", s)], [xml_of_typ T, xml_of_term t]);
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   112
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   113
fun term_of_xml (XML.Elem ("Bound", [("index", i)], [])) = Bound (intofstr "Bound" i)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   114
  | term_of_xml (XML.Elem ("Free", [("name", s)], [T])) = Free (s, typ_of_xml T)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   115
  | term_of_xml (XML.Elem ("Var",[("name", s)], [T])) = Var ((s,0), typ_of_xml T)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   116
  | term_of_xml (XML.Elem ("Var",[("name", s), ("index", i)], [T])) = Var ((s,intofstr "Var" i), typ_of_xml T)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   117
  | term_of_xml (XML.Elem ("Const", [("name", s)], [T])) = Const (s, typ_of_xml T)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   118
  | term_of_xml (XML.Elem ("App", [], [t, u])) = (term_of_xml t) $ (term_of_xml u)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   119
  | term_of_xml (XML.Elem ("Abs", [("vname", s)], [T, t])) = Abs (s, typ_of_xml T, term_of_xml t)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   120
  | term_of_xml _ = parse_err "term"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   121
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   122
fun xml_of_opttypes NONE = []
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   123
  | xml_of_opttypes (SOME Ts) = [XML.Elem ("types", [], map xml_of_typ Ts)];
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   124
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   125
(* FIXME: the t argument of PThm and PAxm is actually redundant, since *)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   126
(* it can be looked up in the theorem database. Thus, it could be      *)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   127
(* omitted from the xml representation.                                *)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   128
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   129
fun xml_of_proof (PBound i) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   130
      XML.Elem ("PBound", [("index", string_of_int i)], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   131
  | xml_of_proof (Abst (s, optT, prf)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   132
      XML.Elem ("Abst", [("vname", s)],
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   133
        (case optT of NONE => [] | SOME T => [xml_of_typ T]) @
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   134
        [xml_of_proof prf])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   135
  | xml_of_proof (AbsP (s, optt, prf)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   136
      XML.Elem ("AbsP", [("vname", s)],
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   137
        (case optt of NONE => [] | SOME t => [xml_of_term t]) @
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   138
        [xml_of_proof prf])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   139
  | xml_of_proof (prf % optt) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   140
      XML.Elem ("Appt", [],
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   141
        xml_of_proof prf ::
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   142
        (case optt of NONE => [] | SOME t => [xml_of_term t]))
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   143
  | xml_of_proof (prf %% prf') =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   144
      XML.Elem ("AppP", [], [xml_of_proof prf, xml_of_proof prf'])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   145
  | xml_of_proof (Hyp t) = XML.Elem ("Hyp", [], [xml_of_term t])
28811
aa36d05926ec adapted PThm and MinProof;
wenzelm
parents: 21858
diff changeset
   146
  | xml_of_proof (PThm (_, ((s, t, optTs), _))) =
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   147
      XML.Elem ("PThm", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   148
  | xml_of_proof (PAxm (s, t, optTs)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   149
      XML.Elem ("PAxm", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   150
  | xml_of_proof (Oracle (s, t, optTs)) =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   151
      XML.Elem ("Oracle", [("name", s)], xml_of_term t :: xml_of_opttypes optTs)
28811
aa36d05926ec adapted PThm and MinProof;
wenzelm
parents: 21858
diff changeset
   152
  | xml_of_proof MinProof = XML.Elem ("MinProof", [], []);
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   153
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   154
fun xml_of_bool b = XML.Elem ("bool", [("value", if b then "true" else "false")], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   155
fun xml_of_int i = XML.Elem ("int", [("value", string_of_int i)], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   156
fun xml_of_string s = XML.Elem ("string", [("value", s)], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   157
fun xml_of_unit () = XML.Elem ("unit", [], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   158
fun xml_of_list output ls = XML.Elem ("list", [], map output ls)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   159
fun xml_of_tuple output ls = XML.Elem ("tuple", [], map output ls)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   160
fun xml_of_option output opt = XML.Elem ("option", [], case opt of NONE => [] | SOME x => [output x])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   161
fun wrap s output x = XML.Elem (s, [], [output x])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   162
fun wrap_empty s = XML.Elem (s, [], [])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   163
fun xml_of_pair output1 output2 (x1, x2) = XML.Elem ("pair", [], [output1 x1, output2 x2])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   164
fun xml_of_triple output1 output2 output3 (x1, x2, x3) = XML.Elem ("triple", [], [output1 x1, output2 x2, output3 x3])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   165
fun xml_of_quadruple output1 output2 output3 output4 (x1, x2, x3, x4) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   166
    XML.Elem ("quadruple", [], [output1 x1, output2 x2, output3 x3, output4 x4])
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   167
fun xml_of_quintuple output1 output2 output3 output4 output5 (x1, x2, x3, x4, x5) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   168
    XML.Elem ("quintuple", [], [output1 x1, output2 x2, output3 x3, output4 x4, output5 x5])
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   169
                                                                                  
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   170
fun bool_of_xml (XML.Elem ("bool", [("value", "true")], [])) = true
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   171
  | bool_of_xml (XML.Elem ("bool", [("value", "false")], [])) = false
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   172
  | bool_of_xml _ = parse_err "bool"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   173
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   174
fun int_of_xml (XML.Elem ("int", [("value", i)], [])) = intofstr "int" i
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   175
  | int_of_xml _ = parse_err "int"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   176
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   177
fun unit_of_xml (XML.Elem ("unit", [], [])) = ()
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   178
  | unit_of_xml _ = parse_err "unit"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   179
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   180
fun list_of_xml input (XML.Elem ("list", [], ls)) = map input ls
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   181
  | list_of_xml _ _ = parse_err "list"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   182
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   183
fun tuple_of_xml input i (XML.Elem ("tuple", [], ls)) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   184
    if i = length ls then map input ls else parse_err "tuple"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   185
  | tuple_of_xml _ _ _ = parse_err "tuple"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   186
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   187
fun option_of_xml input (XML.Elem ("option", [], [])) = NONE
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   188
  | option_of_xml input (XML.Elem ("option", [], [opt])) = SOME (input opt)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   189
  | option_of_xml _ _ = parse_err "option"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   190
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   191
fun pair_of_xml input1 input2 (XML.Elem ("pair", [], [x1, x2])) = (input1 x1, input2 x2)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   192
  | pair_of_xml _ _ _ = parse_err "pair"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   193
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   194
fun triple_of_xml input1 input2 input3 (XML.Elem ("triple", [], [x1, x2, x3])) = (input1 x1, input2 x2, input3 x3)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   195
  | triple_of_xml _ _ _ _ = parse_err "triple"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   196
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   197
fun quadruple_of_xml input1 input2 input3 input4 (XML.Elem ("quadruple", [], [x1, x2, x3, x4])) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   198
    (input1 x1, input2 x2, input3 x3, input4 x4)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   199
  | quadruple_of_xml _ _ _ _ _ = parse_err "quadruple"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   200
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   201
fun quintuple_of_xml input1 input2 input3 input4 input5 (XML.Elem ("quintuple", [], [x1, x2, x3, x4, x5])) = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   202
    (input1 x1, input2 x2, input3 x3, input4 x4, input5 x5)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   203
  | quintuple_of_xml _ _ _ _ _ _ = parse_err "quintuple"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   204
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   205
fun unwrap f input (XML.Elem (_, [], [x])) = f (input x)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   206
  | unwrap _ _ _  = parse_err "unwrap"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   207
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   208
fun unwrap_empty x (XML.Elem (_, [], [])) = x 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   209
  | unwrap_empty _ _ = parse_err "unwrap_unit"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   210
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   211
fun name_of_wrap (XML.Elem (name, _, _)) = name
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   212
  | name_of_wrap _ = parse_err "name_of_wrap"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   213
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   214
fun string_of_xml (XML.Elem ("string", [("value", s)], [])) = s
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   215
  | string_of_xml _ = parse_err "string"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   216
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   217
fun xml_of_mixfix NoSyn = wrap_empty "nosyn"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   218
  | xml_of_mixfix Structure = wrap_empty "structure"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   219
  | xml_of_mixfix (Mixfix args) = wrap "mixfix" (xml_of_triple xml_of_string (xml_of_list xml_of_int) xml_of_int) args
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   220
  | xml_of_mixfix (Delimfix s) = wrap "delimfix" xml_of_string s
35130
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   221
  | xml_of_mixfix (Infix args) = wrap "infix" (xml_of_pair xml_of_string xml_of_int) args
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   222
  | xml_of_mixfix (Infixl args) = wrap "infixl" (xml_of_pair xml_of_string xml_of_int) args
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   223
  | xml_of_mixfix (Infixr args) = wrap "infixr" (xml_of_pair xml_of_string xml_of_int) args
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   224
  | xml_of_mixfix (Binder args) = wrap "binder" (xml_of_triple xml_of_string xml_of_int xml_of_int) args
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   225
                                  
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   226
fun mixfix_of_xml e = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   227
    (case name_of_wrap e of 
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   228
         "nosyn" => unwrap_empty NoSyn 
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   229
       | "structure" => unwrap_empty Structure 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   230
       | "mixfix" => unwrap Mixfix (triple_of_xml string_of_xml (list_of_xml int_of_xml) int_of_xml)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   231
       | "delimfix" => unwrap Delimfix string_of_xml
35130
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   232
       | "infix" => unwrap Infix (pair_of_xml string_of_xml int_of_xml) 
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   233
       | "infixl" => unwrap Infixl (pair_of_xml string_of_xml int_of_xml)  
0991c84e8dcf renamed InfixName to Infix etc.;
wenzelm
parents: 35129
diff changeset
   234
       | "infixr" => unwrap Infixr (pair_of_xml string_of_xml int_of_xml)
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   235
       | "binder" => unwrap Binder (triple_of_xml string_of_xml int_of_xml int_of_xml)
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   236
       | _ => parse_err "mixfix"
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   237
    ) e
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   238
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   239
21858
05f57309170c avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents: 21646
diff changeset
   240
fun to_file f output fname x = File.write (Path.explode fname) (f (output x))
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   241
 
19089
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   242
fun from_file f input fname = 
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   243
    let
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   244
        val _ = writeln "read_from_file enter"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   245
        val s = File.read (Path.explode fname)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   246
        val _ = writeln "done: read file"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   247
        val tree = timeit (fn () => f s)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   248
        val _ = writeln "done: tree"
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   249
        val x = timeit (fn () => input tree)
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   250
        val _ = writeln "done: input"
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   251
    in
32960
69916a850301 eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents: 28811
diff changeset
   252
        x
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   253
    end
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   254
19089
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   255
fun write_to_file x = to_file XML.string_of_tree x
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   256
fun read_from_file x = timeit (fn () => (writeln "read_from_file"; from_file XML.tree_of_string x))
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   257
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   258
fun serialize_to_file x = to_file XML.encoded_string_of_tree x
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   259
fun deserialize_from_file x = timeit (fn () => (writeln "deserialize_from_file"; from_file XML.tree_of_encoded_string x))
2e487fe9593a improved scanning
obua
parents: 19064
diff changeset
   260
19064
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   261
end;
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   262
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   263
structure XMLConvOutput =
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   264
struct
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   265
open XMLConv
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   266
 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   267
val typ = xml_of_typ
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   268
val term = xml_of_term
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   269
val mixfix = xml_of_mixfix
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   270
val proof = xml_of_proof
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   271
val bool = xml_of_bool
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   272
val int = xml_of_int
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   273
val string = xml_of_string
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   274
val unit = xml_of_unit
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   275
val list = xml_of_list
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   276
val pair = xml_of_pair
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   277
val option = xml_of_option
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   278
val triple = xml_of_triple
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   279
val quadruple = xml_of_quadruple
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   280
val quintuple = xml_of_quintuple
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   281
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   282
end
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   283
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   284
structure XMLConvInput = 
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   285
struct
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   286
open XMLConv
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   287
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   288
val typ = typ_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   289
val term = term_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   290
val mixfix = mixfix_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   291
val bool = bool_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   292
val int = int_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   293
val string = string_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   294
val unit = unit_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   295
val list = list_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   296
val pair = pair_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   297
val option = option_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   298
val triple = triple_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   299
val quadruple = quadruple_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   300
val quintuple = quintuple_of_xml
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   301
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   302
end
bf19cc5a7899 fixed bugs, added caching
obua
parents:
diff changeset
   303