author | wenzelm |
Fri, 04 Oct 2019 15:30:52 +0200 | |
changeset 70784 | 799437173553 |
parent 70559 | c92443e8d724 |
child 70786 | d50c8f4f2090 |
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 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
67 |
def itselfT(ty: Typ): Typ = Type(Pure_Thy.ITSELF, List(ty)) |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
68 |
val propT: Typ = Type(Pure_Thy.PROP, Nil) |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
69 |
def funT(ty1: Typ, ty2: Typ): Typ = Type(Pure_Thy.FUN, List(ty1, ty2)) |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
70 |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
71 |
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
|
72 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
73 |
def const_of_class(c: String): String = c + "_class" |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
74 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
75 |
def mk_of_sort(ty: Typ, s: Sort): List[Term] = |
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 |
val t = mk_type(ty) |
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
78 |
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
|
79 |
} |
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
80 |
|
68d2c533db9c
more operations: support type classes within the logic;
wenzelm
parents:
70383
diff
changeset
|
81 |
|
70383
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
82 |
/* type arguments of consts */ |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
83 |
|
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
84 |
def const_typargs(name: String, typ: Typ, typargs: List[String], decl: Typ): List[Typ] = |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
85 |
{ |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
86 |
var subst = Map.empty[String, Typ] |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
87 |
|
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
88 |
def bad_match(): Nothing = error("Malformed type instance for " + name + ": " + typ) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
89 |
def raw_match(arg: (Typ, Typ)) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
90 |
{ |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
91 |
arg match { |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
92 |
case (TFree(a, _), ty) => |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
93 |
subst.get(a) match { |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
94 |
case None => subst += (a -> ty) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
95 |
case Some(ty1) => if (ty != ty1) bad_match() |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
96 |
} |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
97 |
case (Type(c1, args1), Type(c2, args2)) if c1 == c2 => |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
98 |
(args1 zip args2).foreach(raw_match) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
99 |
case _ => bad_match() |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
100 |
} |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
101 |
} |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
102 |
raw_match(decl, typ) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
103 |
|
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
104 |
typargs.map(subst) |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
105 |
} |
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
106 |
|
38ac2e714729
more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents:
68265
diff
changeset
|
107 |
|
68265 | 108 |
|
109 |
/** cache **/ |
|
110 |
||
111 |
def make_cache(initial_size: Int = 131071, max_string: Int = Integer.MAX_VALUE): Cache = |
|
112 |
new Cache(initial_size, max_string) |
|
113 |
||
114 |
class Cache private[Term](initial_size: Int, max_string: Int) |
|
115 |
extends isabelle.Cache(initial_size, max_string) |
|
116 |
{ |
|
117 |
protected def cache_indexname(x: Indexname): Indexname = |
|
70536 | 118 |
lookup(x) getOrElse store(Indexname(cache_string(x.name), x.index)) |
68265 | 119 |
|
120 |
protected def cache_sort(x: Sort): Sort = |
|
121 |
if (x == dummyS) dummyS |
|
122 |
else lookup(x) getOrElse store(x.map(cache_string(_))) |
|
123 |
||
124 |
protected def cache_typ(x: Typ): Typ = |
|
125 |
{ |
|
126 |
if (x == dummyT) dummyT |
|
127 |
else |
|
128 |
lookup(x) match { |
|
129 |
case Some(y) => y |
|
130 |
case None => |
|
131 |
x match { |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
132 |
case Type(name, args) => store(Type(cache_string(name), cache_typs(args))) |
68265 | 133 |
case TFree(name, sort) => store(TFree(cache_string(name), cache_sort(sort))) |
134 |
case TVar(name, sort) => store(TVar(cache_indexname(name), cache_sort(sort))) |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
139 |
protected def cache_typs(x: List[Typ]): List[Typ] = |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
140 |
{ |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
141 |
if (x.isEmpty) Nil |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
142 |
else { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
143 |
lookup(x) match { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
144 |
case Some(y) => y |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
145 |
case None => store(x.map(cache_typ(_))) |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
146 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
147 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
148 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
149 |
|
68265 | 150 |
protected def cache_term(x: Term): Term = |
151 |
{ |
|
152 |
lookup(x) match { |
|
153 |
case Some(y) => y |
|
154 |
case None => |
|
155 |
x match { |
|
70784
799437173553
Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents:
70559
diff
changeset
|
156 |
case Const(name, typargs) => store(Const(cache_string(name), cache_typs(typargs))) |
68265 | 157 |
case Free(name, typ) => store(Free(cache_string(name), cache_typ(typ))) |
158 |
case Var(name, typ) => store(Var(cache_indexname(name), cache_typ(typ))) |
|
70536 | 159 |
case Bound(_) => store(x) |
68265 | 160 |
case Abs(name, typ, body) => |
161 |
store(Abs(cache_string(name), cache_typ(typ), cache_term(body))) |
|
162 |
case App(fun, arg) => store(App(cache_term(fun), cache_term(arg))) |
|
163 |
} |
|
164 |
} |
|
165 |
} |
|
166 |
||
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
167 |
protected def cache_proof(x: Proof): Proof = |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
168 |
{ |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
169 |
if (x == MinProof) MinProof |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
170 |
else { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
171 |
lookup(x) match { |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
172 |
case Some(y) => y |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
173 |
case None => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
174 |
x match { |
70536 | 175 |
case PBound(_) => store(x) |
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
176 |
case Abst(name, typ, body) => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
177 |
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
|
178 |
case AbsP(name, hyp, body) => |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
179 |
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
|
180 |
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
|
181 |
case AppP(fun, arg) => store(AppP(cache_proof(fun), cache_proof(arg))) |
70535 | 182 |
case Hyp(hyp) => store(Hyp(cache_term(hyp))) |
183 |
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
|
184 |
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
|
185 |
case Oracle(name, prop, types) => |
70559 | 186 |
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
|
187 |
case PThm(serial, theory_name, name, types) => |
fc9ba6fe367f
clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents:
70537
diff
changeset
|
188 |
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
|
189 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
190 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
191 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
192 |
} |
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
193 |
|
68265 | 194 |
// main methods |
195 |
def indexname(x: Indexname): Indexname = synchronized { cache_indexname(x) } |
|
196 |
def sort(x: Sort): Sort = synchronized { cache_sort(x) } |
|
197 |
def typ(x: Typ): Typ = synchronized { cache_typ(x) } |
|
198 |
def term(x: Term): Term = synchronized { cache_term(x) } |
|
70533
031620901fcd
support for (fully reconstructed) proof terms in Scala;
wenzelm
parents:
70385
diff
changeset
|
199 |
def proof(x: Proof): Proof = synchronized { cache_proof(x) } |
68265 | 200 |
|
201 |
def position(x: Position.T): Position.T = |
|
202 |
synchronized { x.map({ case (a, b) => (cache_string(a), cache_string(b)) }) } |
|
203 |
} |
|
43731
70072780e095
inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents:
43730
diff
changeset
|
204 |
} |