author | wenzelm |
Sat, 12 Oct 2019 15:01:13 +0200 | |
changeset 70844 | f95a85446a24 |
parent 70842 | c5caf81aa523 |
child 80566 | 446b887e23c7 |
permissions | -rw-r--r-- |
43729 | 1 |
(* Title: Pure/term_xml.ML |
2 |
Author: Makarius |
|
3 |
||
4 |
XML data representation of lambda terms. |
|
5 |
*) |
|
6 |
||
7 |
signature TERM_XML_OPS = |
|
8 |
sig |
|
9 |
type 'a T |
|
70828 | 10 |
type 'a P |
11 |
val indexname: indexname P |
|
43729 | 12 |
val sort: sort T |
70842 | 13 |
val typ: typ T |
70828 | 14 |
val term_raw: term T |
70842 | 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 | 17 |
end |
18 |
||
19 |
signature TERM_XML = |
|
20 |
sig |
|
43779 | 21 |
structure Encode: TERM_XML_OPS |
22 |
structure Decode: TERM_XML_OPS |
|
43729 | 23 |
end; |
24 |
||
25 |
structure Term_XML: TERM_XML = |
|
26 |
struct |
|
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 | 29 |
struct |
30 |
||
43767 | 31 |
open XML.Encode; |
43729 | 32 |
|
70828 | 33 |
fun indexname (a, b) = if b = 0 then [a] else [a, int_atom b]; |
34 |
||
43730
a0ed7bc688b5
lambda terms with XML data representation in Scala;
wenzelm
parents:
43729
diff
changeset
|
35 |
val sort = list string; |
43729 | 36 |
|
70842 | 37 |
fun typ T = T |> variant |
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 | 40 |
fn TVar (a, b) => (indexname a, sort b)]; |
41 |
||
42 |
fun term_raw t = t |> variant |
|
70842 | 43 |
[fn Const (a, b) => ([a], typ b), |
44 |
fn Free (a, b) => ([a], typ b), |
|
45 |
fn Var (a, b) => (indexname a, typ b), |
|
70844 | 46 |
fn Bound a => ([], int a), |
70842 | 47 |
fn Abs (a, b, c) => ([a], pair typ term_raw (b, c)), |
70828 | 48 |
fn op $ a => ([], pair term_raw term_raw a)]; |
49 |
||
70842 | 50 |
fun typ_body T = if T = dummyT then [] else typ T; |
43729 | 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 | 54 |
fn Free (a, b) => ([a], typ_body b), |
55 |
fn Var (a, b) => (indexname a, typ_body b), |
|
70844 | 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 | 60 |
end; |
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 | 63 |
struct |
64 |
||
43767 | 65 |
open XML.Decode; |
43729 | 66 |
|
70828 | 67 |
fun indexname [a] = (a, 0) |
68 |
| indexname [a, b] = (a, int_atom b); |
|
69 |
||
43730
a0ed7bc688b5
lambda terms with XML data representation in Scala;
wenzelm
parents:
43729
diff
changeset
|
70 |
val sort = list string; |
43729 | 71 |
|
70842 | 72 |
fun typ body = body |> variant |
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 | 75 |
fn (a, b) => TVar (indexname a, sort b)]; |
43729 | 76 |
|
70828 | 77 |
fun term_raw body = body |> variant |
70842 | 78 |
[fn ([a], b) => Const (a, typ b), |
79 |
fn ([a], b) => Free (a, typ b), |
|
80 |
fn (a, b) => Var (indexname a, typ b), |
|
70844 | 81 |
fn ([], a) => Bound (int a), |
70842 | 82 |
fn ([a], b) => let val (c, d) = pair typ term_raw b in Abs (a, c, d) end, |
70828 | 83 |
fn ([], a) => op $ (pair term_raw term_raw a)]; |
84 |
||
70842 | 85 |
fun typ_body [] = dummyT |
86 |
| typ_body body = typ body; |
|
70828 | 87 |
|
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 | 90 |
fn ([a], b) => Free (a, typ_body b), |
91 |
fn (a, b) => Var (indexname a, typ_body b), |
|
70844 | 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 | 96 |
end; |
97 |
||
98 |
end; |