43779
|
1 |
/* Title: Pure/term_xml.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
XML data representation of lambda terms.
|
|
5 |
*/
|
|
6 |
|
|
7 |
package isabelle
|
|
8 |
|
|
9 |
|
|
10 |
object Term_XML
|
|
11 |
{
|
|
12 |
import Term._
|
|
13 |
|
|
14 |
object Encode
|
|
15 |
{
|
|
16 |
import XML.Encode._
|
|
17 |
|
|
18 |
val sort: T[Sort] = list(string)
|
|
19 |
|
|
20 |
def typ: T[Typ] =
|
|
21 |
variant[Typ](List(
|
|
22 |
{ case Type(a, b) => (List(a), list(typ)(b)) },
|
|
23 |
{ case TFree(a, b) => (List(a), sort(b)) },
|
|
24 |
{ case TVar((a, b), c) => (List(a, int_atom(b)), sort(c)) }))
|
|
25 |
|
|
26 |
def term: T[Term] =
|
|
27 |
variant[Term](List(
|
|
28 |
{ case Const(a, b) => (List(a), typ(b)) },
|
|
29 |
{ case Free(a, b) => (List(a), typ(b)) },
|
|
30 |
{ case Var((a, b), c) => (List(a, int_atom(b)), typ(c)) },
|
|
31 |
{ case Bound(a) => (List(int_atom(a)), Nil) },
|
|
32 |
{ case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
|
|
33 |
{ case App(a, b) => (Nil, pair(term, term)(a, b)) }))
|
|
34 |
}
|
|
35 |
|
|
36 |
object Decode
|
|
37 |
{
|
|
38 |
import XML.Decode._
|
|
39 |
|
|
40 |
val sort: T[Sort] = list(string)
|
|
41 |
|
|
42 |
def typ: T[Typ] =
|
|
43 |
variant[Typ](List(
|
|
44 |
{ case (List(a), b) => Type(a, list(typ)(b)) },
|
|
45 |
{ case (List(a), b) => TFree(a, sort(b)) },
|
|
46 |
{ case (List(a, b), c) => TVar((a, int_atom(b)), sort(c)) }))
|
|
47 |
|
|
48 |
def term: T[Term] =
|
|
49 |
variant[Term](List(
|
|
50 |
{ case (List(a), b) => Const(a, typ(b)) },
|
|
51 |
{ case (List(a), b) => Free(a, typ(b)) },
|
|
52 |
{ case (List(a, b), c) => Var((a, int_atom(b)), typ(c)) },
|
|
53 |
{ case (List(a), Nil) => Bound(int_atom(a)) },
|
|
54 |
{ case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
|
|
55 |
{ case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
|
|
56 |
}
|
|
57 |
}
|