src/Pure/term_xml.ML
changeset 70828 cb70d84a9f5e
parent 70784 799437173553
child 70842 c5caf81aa523
equal deleted inserted replaced
70827:730251503341 70828:cb70d84a9f5e
     5 *)
     5 *)
     6 
     6 
     7 signature TERM_XML_OPS =
     7 signature TERM_XML_OPS =
     8 sig
     8 sig
     9   type 'a T
     9   type 'a T
       
    10   type 'a P
       
    11   val indexname: indexname P
    10   val sort: sort T
    12   val sort: sort T
       
    13   val typ_raw: typ T
       
    14   val term_raw: term T
    11   val typ: typ T
    15   val typ: typ T
    12   val term: Consts.T -> term T
    16   val term: Consts.T -> term T
    13   val term_raw: term T
       
    14 end
    17 end
    15 
    18 
    16 signature TERM_XML =
    19 signature TERM_XML =
    17 sig
    20 sig
    18   structure Encode: TERM_XML_OPS
    21   structure Encode: TERM_XML_OPS
    25 structure Encode =
    28 structure Encode =
    26 struct
    29 struct
    27 
    30 
    28 open XML.Encode;
    31 open XML.Encode;
    29 
    32 
       
    33 fun indexname (a, b) = if b = 0 then [a] else [a, int_atom b];
       
    34 
    30 val sort = list string;
    35 val sort = list string;
    31 
    36 
    32 fun typ T = T |> variant
    37 fun typ_raw T = T |> variant
    33  [fn Type (a, b) => ([a], list typ b),
    38  [fn Type (a, b) => ([a], list typ_raw b),
    34   fn TFree (a, b) => ([a], sort b),
    39   fn TFree (a, b) => ([a], sort b),
    35   fn TVar ((a, b), c) => ([a, int_atom b], sort c)];
    40   fn TVar (a, b) => (indexname a, sort b)];
       
    41 
       
    42 fun term_raw t = t |> variant
       
    43  [fn Const (a, b) => ([a], typ_raw b),
       
    44   fn Free (a, b) => ([a], typ_raw b),
       
    45   fn Var (a, b) => (indexname a, typ_raw b),
       
    46   fn Bound a => ([int_atom a], []),
       
    47   fn Abs (a, b, c) => ([a], pair typ_raw term_raw (b, c)),
       
    48   fn op $ a => ([], pair term_raw term_raw a)];
       
    49 
       
    50 fun typ T = option typ_raw (if T = dummyT then NONE else SOME T);
    36 
    51 
    37 fun term consts t = t |> variant
    52 fun term consts t = t |> variant
    38  [fn Const (a, b) => ([a], list typ (Consts.typargs consts (a, b))),
    53  [fn Const (a, b) => ([a], list typ (Consts.typargs consts (a, b))),
    39   fn Free (a, b) => ([a], typ b),
    54   fn Free (a, b) => ([a], typ b),
    40   fn Var ((a, b), c) => ([a, int_atom b], typ c),
    55   fn Var (a, b) => (indexname a, typ b),
    41   fn Bound a => ([int_atom a], []),
    56   fn Bound a => ([int_atom a], []),
    42   fn Abs (a, b, c) => ([a], pair typ (term consts) (b, c)),
    57   fn Abs (a, b, c) => ([a], pair typ (term consts) (b, c)),
    43   fn op $ a => ([], pair (term consts) (term consts) a)];
    58   fn op $ a => ([], pair (term consts) (term consts) a)];
    44 
       
    45 fun term_raw t = t |> variant
       
    46  [fn Const (a, b) => ([a], typ b),
       
    47   fn Free (a, b) => ([a], typ b),
       
    48   fn Var ((a, b), c) => ([a, int_atom b], typ c),
       
    49   fn Bound a => ([int_atom a], []),
       
    50   fn Abs (a, b, c) => ([a], pair typ term_raw (b, c)),
       
    51   fn op $ a => ([], pair term_raw term_raw a)];
       
    52 
    59 
    53 end;
    60 end;
    54 
    61 
    55 structure Decode =
    62 structure Decode =
    56 struct
    63 struct
    57 
    64 
    58 open XML.Decode;
    65 open XML.Decode;
    59 
    66 
       
    67 fun indexname [a] = (a, 0)
       
    68   | indexname [a, b] = (a, int_atom b);
       
    69 
    60 val sort = list string;
    70 val sort = list string;
    61 
    71 
    62 fun typ T = T |> variant
    72 fun typ_raw body = body |> variant
    63  [fn ([a], b) => Type (a, list typ b),
    73  [fn ([a], b) => Type (a, list typ_raw b),
    64   fn ([a], b) => TFree (a, sort b),
    74   fn ([a], b) => TFree (a, sort b),
    65   fn ([a, b], c) => TVar ((a, int_atom b), sort c)];
    75   fn (a, b) => TVar (indexname a, sort b)];
    66 
    76 
    67 fun term consts t = t |> variant
    77 fun term_raw body = body |> variant
       
    78  [fn ([a], b) => Const (a, typ_raw b),
       
    79   fn ([a], b) => Free (a, typ_raw b),
       
    80   fn (a, b) => Var (indexname a, typ_raw b),
       
    81   fn ([a], []) => Bound (int_atom a),
       
    82   fn ([a], b) => let val (c, d) = pair typ_raw term_raw b in Abs (a, c, d) end,
       
    83   fn ([], a) => op $ (pair term_raw term_raw a)];
       
    84 
       
    85 val typ = option typ_raw #> the_default dummyT;
       
    86 
       
    87 fun term consts body = body |> variant
    68  [fn ([a], b) => Const (a, Consts.instance consts (a, list typ b)),
    88  [fn ([a], b) => Const (a, Consts.instance consts (a, list typ b)),
    69   fn ([a], b) => Free (a, typ b),
    89   fn ([a], b) => Free (a, typ b),
    70   fn ([a, b], c) => Var ((a, int_atom b), typ c),
    90   fn (a, b) => Var (indexname a, typ b),
    71   fn ([a], []) => Bound (int_atom a),
    91   fn ([a], []) => Bound (int_atom a),
    72   fn ([a], b) => let val (c, d) = pair typ (term consts) b in Abs (a, c, d) end,
    92   fn ([a], b) => let val (c, d) = pair typ (term consts) b in Abs (a, c, d) end,
    73   fn ([], a) => op $ (pair (term consts) (term consts) a)];
    93   fn ([], a) => op $ (pair (term consts) (term consts) a)];
    74 
    94 
    75 fun term_raw t = t |> variant
       
    76  [fn ([a], b) => Const (a, typ b),
       
    77   fn ([a], b) => Free (a, typ b),
       
    78   fn ([a, b], c) => Var ((a, int_atom b), typ c),
       
    79   fn ([a], []) => Bound (int_atom a),
       
    80   fn ([a], b) => let val (c, d) = pair typ term_raw b in Abs (a, c, d) end,
       
    81   fn ([], a) => op $ (pair term_raw term_raw a)];
       
    82 
       
    83 end;
    95 end;
    84 
    96 
    85 end;
    97 end;