author | nipkow |
Tue, 28 Apr 2020 22:55:51 +0200 | |
changeset 71811 | fa4f8f3d69f2 |
parent 71777 | 3875815f5967 |
child 75393 | 87ebf5a50283 |
permissions | -rw-r--r-- |
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 |
||
70828 | 18 |
val indexname: P[Indexname] = |
19 |
{ case Indexname(a, 0) => List(a) |
|
20 |
case Indexname(a, b) => List(a, int_atom(b)) } |
|
21 |
||
43779 | 22 |
val sort: T[Sort] = list(string) |
23 |
||
70842 | 24 |
def typ: T[Typ] = |
43779 | 25 |
variant[Typ](List( |
70842 | 26 |
{ case Type(a, b) => (List(a), list(typ)(b)) }, |
43779 | 27 |
{ case TFree(a, b) => (List(a), sort(b)) }, |
70828 | 28 |
{ case TVar(a, b) => (indexname(a), sort(b)) })) |
29 |
||
70842 | 30 |
val typ_body: T[Typ] = (t: Typ) => if (t == dummyT) Nil else typ(t) |
43779 | 31 |
|
32 |
def term: T[Term] = |
|
33 |
variant[Term](List( |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
34 |
{ case Const(a, b) => (List(a), list(typ)(b)) }, |
70842 | 35 |
{ case Free(a, b) => (List(a), typ_body(b)) }, |
36 |
{ case Var(a, b) => (indexname(a), typ_body(b)) }, |
|
70844 | 37 |
{ case Bound(a) => (Nil, int(a)) }, |
43779 | 38 |
{ case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) }, |
39 |
{ case App(a, b) => (Nil, pair(term, term)(a, b)) })) |
|
40 |
} |
|
41 |
||
42 |
object Decode |
|
43 |
{ |
|
44 |
import XML.Decode._ |
|
45 |
||
70828 | 46 |
val indexname: P[Indexname] = |
47 |
{ case List(a) => Indexname(a, 0) |
|
48 |
case List(a, b) => Indexname(a, int_atom(b)) } |
|
49 |
||
43779 | 50 |
val sort: T[Sort] = list(string) |
51 |
||
70842 | 52 |
def typ: T[Typ] = |
43779 | 53 |
variant[Typ](List( |
70842 | 54 |
{ case (List(a), b) => Type(a, list(typ)(b)) }, |
43779 | 55 |
{ case (List(a), b) => TFree(a, sort(b)) }, |
70828 | 56 |
{ case (a, b) => TVar(indexname(a), sort(b)) })) |
57 |
||
70842 | 58 |
val typ_body: T[Typ] = { case Nil => dummyT case body => typ(body) } |
43779 | 59 |
|
60 |
def term: T[Term] = |
|
61 |
variant[Term](List( |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
62 |
{ case (List(a), b) => Const(a, list(typ)(b)) }, |
70842 | 63 |
{ case (List(a), b) => Free(a, typ_body(b)) }, |
64 |
{ case (a, b) => Var(indexname(a), typ_body(b)) }, |
|
70844 | 65 |
{ case (Nil, a) => Bound(int(a)) }, |
43779 | 66 |
{ case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) }, |
67 |
{ case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) })) |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
43779
diff
changeset
|
68 |
|
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
69 |
def term_env(env: Map[String, Typ]): T[Term] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
70 |
{ |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
71 |
def env_type(x: String, t: Typ): Typ = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
72 |
if (t == dummyT && env.isDefinedAt(x)) env(x) else t |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
73 |
|
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
74 |
def term: T[Term] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
75 |
variant[Term](List( |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
76 |
{ case (List(a), b) => Const(a, list(typ)(b)) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
77 |
{ case (List(a), b) => Free(a, env_type(a, typ_body(b))) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
78 |
{ case (a, b) => Var(indexname(a), typ_body(b)) }, |
70844 | 79 |
{ case (Nil, a) => Bound(int(a)) }, |
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
80 |
{ case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
81 |
{ case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) })) |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
82 |
term |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
83 |
} |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
84 |
|
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
85 |
def proof_env(env: Map[String, Typ]): T[Proof] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
86 |
{ |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
87 |
val term = term_env(env) |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
88 |
def proof: T[Proof] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
89 |
variant[Proof](List( |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
90 |
{ case (Nil, Nil) => MinProof }, |
70844 | 91 |
{ case (Nil, a) => PBound(int(a)) }, |
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
92 |
{ case (List(a), b) => val (c, d) = pair(typ, proof)(b); Abst(a, c, d) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
93 |
{ case (List(a), b) => val (c, d) = pair(term, proof)(b); AbsP(a, c, d) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
94 |
{ case (Nil, a) => val (b, c) = pair(proof, term)(a); Appt(b, c) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
95 |
{ case (Nil, a) => val (b, c) = pair(proof, proof)(a); AppP(b, c) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
96 |
{ case (Nil, a) => Hyp(term(a)) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
97 |
{ case (List(a), b) => PAxm(a, list(typ)(b)) }, |
71777
3875815f5967
clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents:
70844
diff
changeset
|
98 |
{ case (List(a), b) => PClass(typ(b), a) }, |
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
99 |
{ case (List(a), b) => val (c, d) = pair(term, list(typ))(b); Oracle(a, c, d) }, |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
100 |
{ case (List(a, b, c), d) => PThm(long_atom(a), b, c, list(typ)(d)) })) |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
101 |
proof |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
102 |
} |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
103 |
|
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
104 |
val proof: T[Proof] = proof_env(Map.empty) |
43779 | 105 |
} |
106 |
} |