author | wenzelm |
Sun, 19 May 2024 18:43:45 +0200 | |
changeset 80181 | aa92c0f96036 |
parent 75436 | 40630fec3b5d |
child 80295 | 8a9588ffc133 |
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 |
||
75393 | 10 |
object Term_XML { |
43779 | 11 |
import Term._ |
12 |
||
75393 | 13 |
object Encode { |
43779 | 14 |
import XML.Encode._ |
15 |
||
70828 | 16 |
val indexname: P[Indexname] = |
17 |
{ case Indexname(a, 0) => List(a) |
|
18 |
case Indexname(a, b) => List(a, int_atom(b)) } |
|
19 |
||
43779 | 20 |
val sort: T[Sort] = list(string) |
21 |
||
70842 | 22 |
def typ: T[Typ] = |
43779 | 23 |
variant[Typ](List( |
70842 | 24 |
{ case Type(a, b) => (List(a), list(typ)(b)) }, |
43779 | 25 |
{ case TFree(a, b) => (List(a), sort(b)) }, |
70828 | 26 |
{ case TVar(a, b) => (indexname(a), sort(b)) })) |
27 |
||
70842 | 28 |
val typ_body: T[Typ] = (t: Typ) => if (t == dummyT) Nil else typ(t) |
43779 | 29 |
|
30 |
def term: T[Term] = |
|
31 |
variant[Term](List( |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
32 |
{ case Const(a, b) => (List(a), list(typ)(b)) }, |
70842 | 33 |
{ case Free(a, b) => (List(a), typ_body(b)) }, |
34 |
{ case Var(a, b) => (indexname(a), typ_body(b)) }, |
|
70844 | 35 |
{ case Bound(a) => (Nil, int(a)) }, |
43779 | 36 |
{ case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) }, |
37 |
{ case App(a, b) => (Nil, pair(term, term)(a, b)) })) |
|
38 |
} |
|
39 |
||
75393 | 40 |
object Decode { |
43779 | 41 |
import XML.Decode._ |
42 |
||
70828 | 43 |
val indexname: P[Indexname] = |
44 |
{ case List(a) => Indexname(a, 0) |
|
45 |
case List(a, b) => Indexname(a, int_atom(b)) } |
|
46 |
||
43779 | 47 |
val sort: T[Sort] = list(string) |
48 |
||
70842 | 49 |
def typ: T[Typ] = |
43779 | 50 |
variant[Typ](List( |
75436 | 51 |
{ case (List(a), b) => Type(a, list(typ)(b)) }, |
52 |
{ case (List(a), b) => TFree(a, sort(b)) }, |
|
75427 | 53 |
{ case (a, b) => TVar(indexname(a), sort(b)) })) |
70828 | 54 |
|
70842 | 55 |
val typ_body: T[Typ] = { case Nil => dummyT case body => typ(body) } |
43779 | 56 |
|
57 |
def term: T[Term] = |
|
58 |
variant[Term](List( |
|
75436 | 59 |
{ case (List(a), b) => Const(a, list(typ)(b)) }, |
60 |
{ case (List(a), b) => Free(a, typ_body(b)) }, |
|
75427 | 61 |
{ case (a, b) => Var(indexname(a), typ_body(b)) }, |
75436 | 62 |
{ case (Nil, a) => Bound(int(a)) }, |
63 |
{ case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) }, |
|
64 |
{ 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
|
65 |
|
75393 | 66 |
def term_env(env: Map[String, Typ]): T[Term] = { |
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
67 |
def env_type(x: String, t: Typ): Typ = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
68 |
if (t == dummyT && env.isDefinedAt(x)) env(x) else t |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
69 |
|
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
70 |
def term: T[Term] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
71 |
variant[Term](List( |
75436 | 72 |
{ case (List(a), b) => Const(a, list(typ)(b)) }, |
73 |
{ case (List(a), b) => Free(a, env_type(a, typ_body(b))) }, |
|
75427 | 74 |
{ case (a, b) => Var(indexname(a), typ_body(b)) }, |
75436 | 75 |
{ case (Nil, a) => Bound(int(a)) }, |
76 |
{ case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) }, |
|
77 |
{ case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) })) |
|
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
78 |
term |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
79 |
} |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
80 |
|
75393 | 81 |
def proof_env(env: Map[String, Typ]): T[Proof] = { |
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
82 |
val term = term_env(env) |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
83 |
def proof: T[Proof] = |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
84 |
variant[Proof](List( |
75436 | 85 |
{ case (Nil, Nil) => MinProof }, |
86 |
{ case (Nil, a) => PBound(int(a)) }, |
|
87 |
{ case (List(a), b) => val (c, d) = pair(typ, proof)(b); Abst(a, c, d) }, |
|
88 |
{ case (List(a), b) => val (c, d) = pair(term, proof)(b); AbsP(a, c, d) }, |
|
89 |
{ case (Nil, a) => val (b, c) = pair(proof, term)(a); Appt(b, c) }, |
|
90 |
{ case (Nil, a) => val (b, c) = pair(proof, proof)(a); AppP(b, c) }, |
|
91 |
{ case (Nil, a) => Hyp(term(a)) }, |
|
92 |
{ case (List(a), b) => PAxm(a, list(typ)(b)) }, |
|
93 |
{ case (List(a), b) => PClass(typ(b), a) }, |
|
94 |
{ case (List(a), b) => val (c, d) = pair(term, list(typ))(b); Oracle(a, c, d) }, |
|
95 |
{ case (List(a, b, c), d) => PThm(long_atom(a), b, c, list(typ)(d)) })) |
|
70843
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
96 |
proof |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
97 |
} |
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
98 |
|
cc987440d776
more compact XML: separate environment for free variables;
wenzelm
parents:
70842
diff
changeset
|
99 |
val proof: T[Proof] = proof_env(Map.empty) |
43779 | 100 |
} |
101 |
} |