| author | wenzelm | 
| Sun, 05 Jan 2025 15:04:42 +0100 | |
| changeset 81727 | 4ab59fef89ea | 
| parent 80589 | 7849b6370425 | 
| 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 | 
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 15 | val term: Consts.T -> term T | 
| 43729 | 16 | end | 
| 17 | ||
| 18 | signature TERM_XML = | |
| 19 | sig | |
| 43779 | 20 | structure Encode: TERM_XML_OPS | 
| 21 | structure Decode: TERM_XML_OPS | |
| 43729 | 22 | end; | 
| 23 | ||
| 24 | structure Term_XML: TERM_XML = | |
| 25 | struct | |
| 26 | ||
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43730diff
changeset | 27 | structure Encode = | 
| 43729 | 28 | struct | 
| 29 | ||
| 43767 | 30 | open XML.Encode; | 
| 43729 | 31 | |
| 70828 | 32 | fun indexname (a, b) = if b = 0 then [a] else [a, int_atom b]; | 
| 33 | ||
| 43730 
a0ed7bc688b5
lambda terms with XML data representation in Scala;
 wenzelm parents: 
43729diff
changeset | 34 | val sort = list string; | 
| 43729 | 35 | |
| 70842 | 36 | fun typ T = T |> variant | 
| 37 | [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: 
43767diff
changeset | 38 | fn TFree (a, b) => ([a], sort b), | 
| 70828 | 39 | fn TVar (a, b) => (indexname a, sort b)]; | 
| 40 | ||
| 41 | fun term_raw t = t |> variant | |
| 70842 | 42 | [fn Const (a, b) => ([a], typ b), | 
| 43 | fn Free (a, b) => ([a], typ b), | |
| 44 | fn Var (a, b) => (indexname a, typ b), | |
| 70844 | 45 | fn Bound a => ([], int a), | 
| 70842 | 46 | fn Abs (a, b, c) => ([a], pair typ term_raw (b, c)), | 
| 70828 | 47 | fn op $ a => ([], pair term_raw term_raw a)]; | 
| 48 | ||
| 80589 | 49 | fun var_type T = if T = dummyT then [] else typ T; | 
| 43729 | 50 | |
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 51 | fun term consts t = t |> variant | 
| 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 52 | [fn Const (a, b) => ([a], list typ (Consts.typargs consts (a, b))), | 
| 80589 | 53 | fn Free (a, b) => ([a], var_type b), | 
| 54 | fn Var (a, b) => (indexname a, var_type b), | |
| 70844 | 55 | fn Bound a => ([], int a), | 
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 56 | fn Abs (a, b, c) => ([a], pair typ (term consts) (b, c)), | 
| 80566 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 57 | fn t as op $ a => | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 58 | if can Logic.match_of_class t then raise Match | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 59 | else ([], pair (term consts) (term consts) a), | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 60 | fn t => | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 61 | let val (T, c) = Logic.match_of_class t | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 62 | in ([c], typ T) end]; | 
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 63 | |
| 43729 | 64 | end; | 
| 65 | ||
| 43731 
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
 wenzelm parents: 
43730diff
changeset | 66 | structure Decode = | 
| 43729 | 67 | struct | 
| 68 | ||
| 43767 | 69 | open XML.Decode; | 
| 43729 | 70 | |
| 70828 | 71 | fun indexname [a] = (a, 0) | 
| 72 | | indexname [a, b] = (a, int_atom b); | |
| 73 | ||
| 43730 
a0ed7bc688b5
lambda terms with XML data representation in Scala;
 wenzelm parents: 
43729diff
changeset | 74 | val sort = list string; | 
| 43729 | 75 | |
| 70842 | 76 | fun typ body = body |> variant | 
| 77 | [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: 
43767diff
changeset | 78 | fn ([a], b) => TFree (a, sort b), | 
| 70828 | 79 | fn (a, b) => TVar (indexname a, sort b)]; | 
| 43729 | 80 | |
| 70828 | 81 | fun term_raw body = body |> variant | 
| 70842 | 82 | [fn ([a], b) => Const (a, typ b), | 
| 83 | fn ([a], b) => Free (a, typ b), | |
| 84 | fn (a, b) => Var (indexname a, typ b), | |
| 70844 | 85 | fn ([], a) => Bound (int a), | 
| 70842 | 86 | fn ([a], b) => let val (c, d) = pair typ term_raw b in Abs (a, c, d) end, | 
| 70828 | 87 | fn ([], a) => op $ (pair term_raw term_raw a)]; | 
| 88 | ||
| 80589 | 89 | fun var_type [] = dummyT | 
| 90 | | var_type body = typ body; | |
| 70828 | 91 | |
| 92 | fun term consts body = body |> variant | |
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 93 | [fn ([a], b) => Const (a, Consts.instance consts (a, list typ b)), | 
| 80589 | 94 | fn ([a], b) => Free (a, var_type b), | 
| 95 | fn (a, b) => Var (indexname a, var_type b), | |
| 70844 | 96 | fn ([], a) => Bound (int a), | 
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 97 | fn ([a], b) => let val (c, d) = pair typ (term consts) b in Abs (a, c, d) end, | 
| 80566 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 98 | fn ([], a) => op $ (pair (term consts) (term consts) a), | 
| 
446b887e23c7
clarified data representation: prefer explicit OFCLASS constructor, following datatype zterm;
 wenzelm parents: 
70844diff
changeset | 99 | fn ([a], b) => Logic.mk_of_class (typ b, a)]; | 
| 70784 
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
 wenzelm parents: 
43789diff
changeset | 100 | |
| 43729 | 101 | end; | 
| 102 | ||
| 103 | end; |