src/Pure/term_xml.ML
author wenzelm
Sat, 12 Oct 2019 15:01:13 +0200
changeset 70844 f95a85446a24
parent 70842 c5caf81aa523
child 80566 446b887e23c7
permissions -rw-r--r--
more compact XML;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/term_xml.ML
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     3
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     4
XML data representation of lambda terms.
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     5
*)
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     6
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     7
signature TERM_XML_OPS =
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     8
sig
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
     9
  type 'a T
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    10
  type 'a P
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    11
  val indexname: indexname P
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    12
  val sort: sort T
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    13
  val typ: typ T
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    14
  val term_raw: term T
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    15
  val typ_body: typ T
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    16
  val term: Consts.T -> term T
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    17
end
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    18
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    19
signature TERM_XML =
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    20
sig
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents: 43778
diff changeset
    21
  structure Encode: TERM_XML_OPS
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents: 43778
diff changeset
    22
  structure Decode: TERM_XML_OPS
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    23
end;
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    24
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    25
structure Term_XML: TERM_XML =
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    26
struct
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    27
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43730
diff changeset
    28
structure Encode =
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    29
struct
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    30
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43731
diff changeset
    31
open XML.Encode;
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    32
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    33
fun indexname (a, b) = if b = 0 then [a] else [a, int_atom b];
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    34
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents: 43729
diff changeset
    35
val sort = list string;
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    36
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    37
fun typ T = T |> variant
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    38
 [fn Type (a, b) => ([a], list typ b),
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43767
diff changeset
    39
  fn TFree (a, b) => ([a], sort b),
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    40
  fn TVar (a, b) => (indexname a, sort b)];
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    41
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    42
fun term_raw t = t |> variant
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    43
 [fn Const (a, b) => ([a], typ b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    44
  fn Free (a, b) => ([a], typ b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    45
  fn Var (a, b) => (indexname a, typ b),
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70842
diff changeset
    46
  fn Bound a => ([], int a),
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    47
  fn Abs (a, b, c) => ([a], pair typ term_raw (b, c)),
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    48
  fn op $ a => ([], pair term_raw term_raw a)];
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    49
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    50
fun typ_body T = if T = dummyT then [] else typ T;
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    51
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    52
fun term consts t = t |> variant
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    53
 [fn Const (a, b) => ([a], list typ (Consts.typargs consts (a, b))),
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    54
  fn Free (a, b) => ([a], typ_body b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    55
  fn Var (a, b) => (indexname a, typ_body b),
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70842
diff changeset
    56
  fn Bound a => ([], int a),
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    57
  fn Abs (a, b, c) => ([a], pair typ (term consts) (b, c)),
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    58
  fn op $ a => ([], pair (term consts) (term consts) a)];
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    59
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    60
end;
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    61
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43730
diff changeset
    62
structure Decode =
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    63
struct
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    64
43767
e0219ef7f84c tuned XML modules;
wenzelm
parents: 43731
diff changeset
    65
open XML.Decode;
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    66
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    67
fun indexname [a] = (a, 0)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    68
  | indexname [a, b] = (a, int_atom b);
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    69
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents: 43729
diff changeset
    70
val sort = list string;
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    71
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    72
fun typ body = body |> variant
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    73
 [fn ([a], b) => Type (a, list typ b),
43778
ce9189450447 more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
wenzelm
parents: 43767
diff changeset
    74
  fn ([a], b) => TFree (a, sort b),
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    75
  fn (a, b) => TVar (indexname a, sort b)];
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    76
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    77
fun term_raw body = body |> variant
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    78
 [fn ([a], b) => Const (a, typ b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    79
  fn ([a], b) => Free (a, typ b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    80
  fn (a, b) => Var (indexname a, typ b),
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70842
diff changeset
    81
  fn ([], a) => Bound (int a),
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    82
  fn ([a], b) => let val (c, d) = pair typ term_raw b in Abs (a, c, d) end,
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    83
  fn ([], a) => op $ (pair term_raw term_raw a)];
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    84
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    85
fun typ_body [] = dummyT
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    86
  | typ_body body = typ body;
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    87
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    88
fun term consts body = body |> variant
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    89
 [fn ([a], b) => Const (a, Consts.instance consts (a, list typ b)),
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    90
  fn ([a], b) => Free (a, typ_body b),
c5caf81aa523 more compact XML;
wenzelm
parents: 70828
diff changeset
    91
  fn (a, b) => Var (indexname a, typ_body b),
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70842
diff changeset
    92
  fn ([], a) => Bound (int a),
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    93
  fn ([a], b) => let val (c, d) = pair typ (term consts) b in Abs (a, c, d) end,
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    94
  fn ([], a) => op $ (pair (term consts) (term consts) a)];
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 43789
diff changeset
    95
43729
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    96
end;
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    97
07d3c6afa865 XML data representation of lambda terms;
wenzelm
parents:
diff changeset
    98
end;