69240
|
1 |
{- generated by Isabelle -}
|
|
2 |
|
|
3 |
{- Title: Tools/Haskell/XML/Encode.hs
|
|
4 |
Author: Makarius
|
|
5 |
LICENSE: BSD 3-clause (Isabelle)
|
|
6 |
|
|
7 |
XML as data representation language.
|
|
8 |
-}
|
|
9 |
|
|
10 |
module Isabelle.XML.Encode (
|
|
11 |
A, T, V,
|
|
12 |
|
|
13 |
int_atom, bool_atom, unit_atom,
|
|
14 |
|
|
15 |
tree, properties, string, init, bool, unit, pair, triple, list, variant
|
|
16 |
)
|
|
17 |
where
|
|
18 |
|
|
19 |
import Isabelle.Library
|
|
20 |
import qualified Isabelle.Value as Value
|
|
21 |
import qualified Isabelle.Properties as Properties
|
|
22 |
import qualified Isabelle.XML as XML
|
|
23 |
|
|
24 |
|
|
25 |
type A a = a -> String
|
|
26 |
type T a = a -> XML.Body
|
|
27 |
type V a = a -> Maybe ([String], XML.Body)
|
|
28 |
|
|
29 |
|
|
30 |
-- atomic values
|
|
31 |
|
|
32 |
int_atom :: A Int
|
|
33 |
int_atom = Value.print_int
|
|
34 |
|
|
35 |
bool_atom :: A Bool
|
|
36 |
bool_atom False = "0"
|
|
37 |
bool_atom True = "1"
|
|
38 |
|
|
39 |
unit_atom :: A ()
|
|
40 |
unit_atom () = ""
|
|
41 |
|
|
42 |
|
|
43 |
-- structural nodes
|
|
44 |
|
|
45 |
node = XML.Elem (":", [])
|
|
46 |
|
|
47 |
vector = map_index (\(i, x) -> (int_atom i, x))
|
|
48 |
|
|
49 |
tagged (tag, (xs, ts)) = XML.Elem (int_atom tag, vector xs) ts
|
|
50 |
|
|
51 |
|
|
52 |
-- representation of standard types
|
|
53 |
|
|
54 |
tree :: T XML.Tree
|
|
55 |
tree t = [t]
|
|
56 |
|
|
57 |
properties :: T Properties.T
|
|
58 |
properties props = [XML.Elem (":", props) []]
|
|
59 |
|
|
60 |
string :: T String
|
|
61 |
string "" = []
|
|
62 |
string s = [XML.Text s]
|
|
63 |
|
|
64 |
int :: T Int
|
|
65 |
int = string . int_atom
|
|
66 |
|
|
67 |
bool :: T Bool
|
|
68 |
bool = string . bool_atom
|
|
69 |
|
|
70 |
unit :: T ()
|
|
71 |
unit = string . unit_atom
|
|
72 |
|
|
73 |
pair :: T a -> T b -> T (a, b)
|
|
74 |
pair f g (x, y) = [node (f x), node (g y)]
|
|
75 |
|
|
76 |
triple :: T a -> T b -> T c -> T (a, b, c)
|
|
77 |
triple f g h (x, y, z) = [node (f x), node (g y), node (h z)]
|
|
78 |
|
|
79 |
list :: T a -> T [a]
|
|
80 |
list f xs = map (node . f) xs
|
|
81 |
|
|
82 |
variant :: [V a] -> T a
|
|
83 |
variant fs x = [tagged (the (get_index (\f -> f x) fs))]
|