author | wenzelm |
Thu, 10 Oct 2019 14:55:26 +0200 | |
changeset 70823 | c6f2a73987cd |
parent 70786 | d50c8f4f2090 |
child 70834 | 614ca81fa28e |
permissions | -rw-r--r-- |
43730 | 1 |
/* Title: Pure/term.scala |
2 |
Author: Makarius |
|
3 |
||
43779 | 4 |
Lambda terms, types, sorts. |
43730 | 5 |
|
6 |
Note: Isabelle/ML is the primary environment for logical operations. |
|
7 |
*/ |
|
8 |
||
9 |
package isabelle |
|
10 |
||
11 |
||
12 |
object Term |
|
13 |
{ |
|
70383
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
14 |
/* types and terms */ |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
15 |
|
70536 | 16 |
sealed case class Indexname(name: String, index: Int) |
70537
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
17 |
{ |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
18 |
override def toString: String = |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
19 |
if (index == -1) name |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
20 |
else { |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
21 |
val dot = |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
22 |
Symbol.explode(name).reverse match { |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
23 |
case _ :: s :: _ if s == Symbol.sub_decoded || s == Symbol.sub => false |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
24 |
case c :: _ => Symbol.is_digit(c) |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
25 |
case _ => true |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
26 |
} |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
27 |
if (dot) "?" + name + "." + index |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
28 |
else if (index != 0) "?" + name + index |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
29 |
else "?" + name |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
30 |
} |
17160e0a60b6
Indexname.toString according to string_of_vname' in ML;
wenzelm
parents:
70536
diff
changeset
|
31 |
} |
43730 | 32 |
|
33 |
type Sort = List[String] |
|
34 |
val dummyS: Sort = List("") |
|
35 |
||
36 |
sealed abstract class Typ |
|
37 |
case class Type(name: String, args: List[Typ] = Nil) extends Typ |
|
38 |
case class TFree(name: String, sort: Sort = dummyS) extends Typ |
|
39 |
case class TVar(name: Indexname, sort: Sort = dummyS) extends Typ |
|
40 |
val dummyT = Type("dummy") |
|
41 |
||
42 |
sealed abstract class Term |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
43 |
case class Const(name: String, typargs: List[Typ]) extends Term |
43730 | 44 |
case class Free(name: String, typ: Typ = dummyT) extends Term |
45 |
case class Var(name: Indexname, typ: Typ = dummyT) extends Term |
|
46 |
case class Bound(index: Int) extends Term |
|
47 |
case class Abs(name: String, typ: Typ = dummyT, body: Term) extends Term |
|
48 |
case class App(fun: Term, arg: Term) extends Term |
|
68265 | 49 |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
50 |
sealed abstract class Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
51 |
case object MinProof extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
52 |
case class PBound(index: Int) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
53 |
case class Abst(name: String, typ: Typ, body: Proof) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
54 |
case class AbsP(name: String, hyp: Term, body: Proof) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
55 |
case class Appt(fun: Proof, arg: Term) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
56 |
case class AppP(fun: Proof, arg: Proof) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
57 |
case class Hyp(hyp: Term) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
58 |
case class PAxm(name: String, types: List[Typ]) extends Proof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
59 |
case class OfClass(typ: Typ, cls: String) extends Proof |
70559 | 60 |
case class Oracle(name: String, prop: Option[Term], types: List[Typ]) extends Proof |
70554 | 61 |
case class PThm(serial: Long, theory_name: String, approximative_name: String, types: List[Typ]) |
70538
fc9ba6fe367f
clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents:
70537
diff
changeset
|
62 |
extends Proof |
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
63 |
|
68265 | 64 |
|
70385
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
65 |
/* Pure logic */ |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
66 |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
67 |
def mk_type(ty: Typ): Term = Const(Pure_Thy.TYPE, List(ty)) |
70385
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
68 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
69 |
def const_of_class(c: String): String = c + "_class" |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
70 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
71 |
def mk_of_sort(ty: Typ, s: Sort): List[Term] = |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
72 |
{ |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
73 |
val t = mk_type(ty) |
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
74 |
s.map(c => App(Const(const_of_class(c), List(ty)), t)) |
70385
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
75 |
} |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
76 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
77 |
|
68265 | 78 |
|
79 |
/** cache **/ |
|
80 |
||
81 |
def make_cache(initial_size: Int = 131071, max_string: Int = Integer.MAX_VALUE): Cache = |
|
82 |
new Cache(initial_size, max_string) |
|
83 |
||
84 |
class Cache private[Term](initial_size: Int, max_string: Int) |
|
85 |
extends isabelle.Cache(initial_size, max_string) |
|
86 |
{ |
|
87 |
protected def cache_indexname(x: Indexname): Indexname = |
|
70536 | 88 |
lookup(x) getOrElse store(Indexname(cache_string(x.name), x.index)) |
68265 | 89 |
|
90 |
protected def cache_sort(x: Sort): Sort = |
|
91 |
if (x == dummyS) dummyS |
|
92 |
else lookup(x) getOrElse store(x.map(cache_string(_))) |
|
93 |
||
94 |
protected def cache_typ(x: Typ): Typ = |
|
95 |
{ |
|
96 |
if (x == dummyT) dummyT |
|
97 |
else |
|
98 |
lookup(x) match { |
|
99 |
case Some(y) => y |
|
100 |
case None => |
|
101 |
x match { |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
102 |
case Type(name, args) => store(Type(cache_string(name), cache_typs(args))) |
68265 | 103 |
case TFree(name, sort) => store(TFree(cache_string(name), cache_sort(sort))) |
104 |
case TVar(name, sort) => store(TVar(cache_indexname(name), cache_sort(sort))) |
|
105 |
} |
|
106 |
} |
|
107 |
} |
|
108 |
||
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
109 |
protected def cache_typs(x: List[Typ]): List[Typ] = |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
110 |
{ |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
111 |
if (x.isEmpty) Nil |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
112 |
else { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
113 |
lookup(x) match { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
114 |
case Some(y) => y |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
115 |
case None => store(x.map(cache_typ(_))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
116 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
117 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
118 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
119 |
|
68265 | 120 |
protected def cache_term(x: Term): Term = |
121 |
{ |
|
122 |
lookup(x) match { |
|
123 |
case Some(y) => y |
|
124 |
case None => |
|
125 |
x match { |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
126 |
case Const(name, typargs) => store(Const(cache_string(name), cache_typs(typargs))) |
68265 | 127 |
case Free(name, typ) => store(Free(cache_string(name), cache_typ(typ))) |
128 |
case Var(name, typ) => store(Var(cache_indexname(name), cache_typ(typ))) |
|
70536 | 129 |
case Bound(_) => store(x) |
68265 | 130 |
case Abs(name, typ, body) => |
131 |
store(Abs(cache_string(name), cache_typ(typ), cache_term(body))) |
|
132 |
case App(fun, arg) => store(App(cache_term(fun), cache_term(arg))) |
|
133 |
} |
|
134 |
} |
|
135 |
} |
|
136 |
||
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
137 |
protected def cache_proof(x: Proof): Proof = |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
138 |
{ |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
139 |
if (x == MinProof) MinProof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
140 |
else { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
141 |
lookup(x) match { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
142 |
case Some(y) => y |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
143 |
case None => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
144 |
x match { |
70536 | 145 |
case PBound(_) => store(x) |
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
146 |
case Abst(name, typ, body) => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
147 |
store(Abst(cache_string(name), cache_typ(typ), cache_proof(body))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
148 |
case AbsP(name, hyp, body) => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
149 |
store(AbsP(cache_string(name), cache_term(hyp), cache_proof(body))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
150 |
case Appt(fun, arg) => store(Appt(cache_proof(fun), cache_term(arg))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
151 |
case AppP(fun, arg) => store(AppP(cache_proof(fun), cache_proof(arg))) |
70535 | 152 |
case Hyp(hyp) => store(Hyp(cache_term(hyp))) |
153 |
case PAxm(name, types) => store(PAxm(cache_string(name), cache_typs(types))) |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
154 |
case OfClass(typ, cls) => store(OfClass(cache_typ(typ), cache_string(cls))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
155 |
case Oracle(name, prop, types) => |
70559 | 156 |
store(Oracle(cache_string(name), prop.map(cache_term(_)), cache_typs(types))) |
70538
fc9ba6fe367f
clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents:
70537
diff
changeset
|
157 |
case PThm(serial, theory_name, name, types) => |
fc9ba6fe367f
clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents:
70537
diff
changeset
|
158 |
store(PThm(serial, cache_string(theory_name), cache_string(name), cache_typs(types))) |
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
159 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
160 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
161 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
162 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
163 |
|
68265 | 164 |
// main methods |
165 |
def indexname(x: Indexname): Indexname = synchronized { cache_indexname(x) } |
|
166 |
def sort(x: Sort): Sort = synchronized { cache_sort(x) } |
|
167 |
def typ(x: Typ): Typ = synchronized { cache_typ(x) } |
|
168 |
def term(x: Term): Term = synchronized { cache_term(x) } |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
169 |
def proof(x: Proof): Proof = synchronized { cache_proof(x) } |
68265 | 170 |
|
171 |
def position(x: Position.T): Position.T = |
|
172 |
synchronized { x.map({ case (a, b) => (cache_string(a), cache_string(b)) }) } |
|
173 |
} |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43730
diff
changeset
|
174 |
} |