src/HOL/Import/xmlconv.ML
changeset 32960 69916a850301
parent 28811 aa36d05926ec
child 35129 ed24ba6f69aa
equal deleted inserted replaced
32959:23a8c5ac35f8 32960:69916a850301
    16   
    16   
    17   val xml_of_proof: Proofterm.proof output
    17   val xml_of_proof: Proofterm.proof output
    18 
    18 
    19   val xml_of_bool: bool output
    19   val xml_of_bool: bool output
    20   val bool_of_xml: bool input
    20   val bool_of_xml: bool input
    21 		  
    21                   
    22   val xml_of_int: int output
    22   val xml_of_int: int output
    23   val int_of_xml: int input
    23   val int_of_xml: int input
    24 
    24 
    25   val xml_of_string: string output
    25   val xml_of_string: string output
    26   val string_of_xml: string input
    26   val string_of_xml: string input
    84   | xml_of_typ (Type (s, Ts)) =
    84   | xml_of_typ (Type (s, Ts)) =
    85       XML.Elem ("Type", [("name", s)], map xml_of_typ Ts);
    85       XML.Elem ("Type", [("name", s)], map xml_of_typ Ts);
    86 
    86 
    87 fun intofstr s i = 
    87 fun intofstr s i = 
    88     case Int.fromString i of 
    88     case Int.fromString i of 
    89 	NONE => parse_err (s^", invalid index: "^i)
    89         NONE => parse_err (s^", invalid index: "^i)
    90       | SOME i => i
    90       | SOME i => i
    91 
    91 
    92 fun typ_of_xml (XML.Elem ("TVar", [("name", s)], cs)) = TVar ((s,0), map class_of_xml cs)
    92 fun typ_of_xml (XML.Elem ("TVar", [("name", s)], cs)) = TVar ((s,0), map class_of_xml cs)
    93   | typ_of_xml (XML.Elem ("TVar", [("name", s), ("index", i)], cs)) = 
    93   | typ_of_xml (XML.Elem ("TVar", [("name", s), ("index", i)], cs)) = 
    94     TVar ((s, intofstr "TVar" i), map class_of_xml cs)
    94     TVar ((s, intofstr "TVar" i), map class_of_xml cs)
   164 fun xml_of_triple output1 output2 output3 (x1, x2, x3) = XML.Elem ("triple", [], [output1 x1, output2 x2, output3 x3])
   164 fun xml_of_triple output1 output2 output3 (x1, x2, x3) = XML.Elem ("triple", [], [output1 x1, output2 x2, output3 x3])
   165 fun xml_of_quadruple output1 output2 output3 output4 (x1, x2, x3, x4) = 
   165 fun xml_of_quadruple output1 output2 output3 output4 (x1, x2, x3, x4) = 
   166     XML.Elem ("quadruple", [], [output1 x1, output2 x2, output3 x3, output4 x4])
   166     XML.Elem ("quadruple", [], [output1 x1, output2 x2, output3 x3, output4 x4])
   167 fun xml_of_quintuple output1 output2 output3 output4 output5 (x1, x2, x3, x4, x5) = 
   167 fun xml_of_quintuple output1 output2 output3 output4 output5 (x1, x2, x3, x4, x5) = 
   168     XML.Elem ("quintuple", [], [output1 x1, output2 x2, output3 x3, output4 x4, output5 x5])
   168     XML.Elem ("quintuple", [], [output1 x1, output2 x2, output3 x3, output4 x4, output5 x5])
   169 										  
   169                                                                                   
   170 fun bool_of_xml (XML.Elem ("bool", [("value", "true")], [])) = true
   170 fun bool_of_xml (XML.Elem ("bool", [("value", "true")], [])) = true
   171   | bool_of_xml (XML.Elem ("bool", [("value", "false")], [])) = false
   171   | bool_of_xml (XML.Elem ("bool", [("value", "false")], [])) = false
   172   | bool_of_xml _ = parse_err "bool"
   172   | bool_of_xml _ = parse_err "bool"
   173 
   173 
   174 fun int_of_xml (XML.Elem ("int", [("value", i)], [])) = intofstr "int" i
   174 fun int_of_xml (XML.Elem ("int", [("value", i)], [])) = intofstr "int" i
   223   | xml_of_mixfix (InfixrName args) = wrap "infixrname" (xml_of_pair xml_of_string xml_of_int) args
   223   | xml_of_mixfix (InfixrName args) = wrap "infixrname" (xml_of_pair xml_of_string xml_of_int) args
   224   | xml_of_mixfix (Infix i) = wrap "infix" xml_of_int i
   224   | xml_of_mixfix (Infix i) = wrap "infix" xml_of_int i
   225   | xml_of_mixfix (Infixl i) = wrap "infixl" xml_of_int i
   225   | xml_of_mixfix (Infixl i) = wrap "infixl" xml_of_int i
   226   | xml_of_mixfix (Infixr i) = wrap "infixr" xml_of_int i
   226   | xml_of_mixfix (Infixr i) = wrap "infixr" xml_of_int i
   227   | xml_of_mixfix (Binder args) = wrap "binder" (xml_of_triple xml_of_string xml_of_int xml_of_int) args
   227   | xml_of_mixfix (Binder args) = wrap "binder" (xml_of_triple xml_of_string xml_of_int xml_of_int) args
   228 				  
   228                                   
   229 fun mixfix_of_xml e = 
   229 fun mixfix_of_xml e = 
   230     (case name_of_wrap e of 
   230     (case name_of_wrap e of 
   231 	 "nosyn" => unwrap_empty NoSyn 
   231          "nosyn" => unwrap_empty NoSyn 
   232        | "structure" => unwrap_empty Structure 
   232        | "structure" => unwrap_empty Structure 
   233        | "mixfix" => unwrap Mixfix (triple_of_xml string_of_xml (list_of_xml int_of_xml) int_of_xml)
   233        | "mixfix" => unwrap Mixfix (triple_of_xml string_of_xml (list_of_xml int_of_xml) int_of_xml)
   234        | "delimfix" => unwrap Delimfix string_of_xml
   234        | "delimfix" => unwrap Delimfix string_of_xml
   235        | "infixname" => unwrap InfixName (pair_of_xml string_of_xml int_of_xml) 
   235        | "infixname" => unwrap InfixName (pair_of_xml string_of_xml int_of_xml) 
   236        | "infixlname" => unwrap InfixlName (pair_of_xml string_of_xml int_of_xml)  
   236        | "infixlname" => unwrap InfixlName (pair_of_xml string_of_xml int_of_xml)  
   245 
   245 
   246 fun to_file f output fname x = File.write (Path.explode fname) (f (output x))
   246 fun to_file f output fname x = File.write (Path.explode fname) (f (output x))
   247  
   247  
   248 fun from_file f input fname = 
   248 fun from_file f input fname = 
   249     let
   249     let
   250 	val _ = writeln "read_from_file enter"
   250         val _ = writeln "read_from_file enter"
   251 	val s = File.read (Path.explode fname)
   251         val s = File.read (Path.explode fname)
   252 	val _ = writeln "done: read file"
   252         val _ = writeln "done: read file"
   253 	val tree = timeit (fn () => f s)
   253         val tree = timeit (fn () => f s)
   254 	val _ = writeln "done: tree"
   254         val _ = writeln "done: tree"
   255 	val x = timeit (fn () => input tree)
   255         val x = timeit (fn () => input tree)
   256 	val _ = writeln "done: input"
   256         val _ = writeln "done: input"
   257     in
   257     in
   258 	x
   258         x
   259     end
   259     end
   260 
   260 
   261 fun write_to_file x = to_file XML.string_of_tree x
   261 fun write_to_file x = to_file XML.string_of_tree x
   262 fun read_from_file x = timeit (fn () => (writeln "read_from_file"; from_file XML.tree_of_string x))
   262 fun read_from_file x = timeit (fn () => (writeln "read_from_file"; from_file XML.tree_of_string x))
   263 
   263