src/Pure/term.scala
author wenzelm
Fri, 01 Apr 2022 17:06:10 +0200
changeset 75393 87ebf5a50283
parent 74731 161e84e6b40a
child 76351 2cee31cd92f0
permissions -rw-r--r--
clarified formatting, for the sake of scala3;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/term.scala
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     3
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents: 43778
diff changeset
     4
Lambda terms, types, sorts.
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     5
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     6
Note: Isabelle/ML is the primary environment for logical operations.
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     7
*/
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     8
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
     9
package isabelle
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    10
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    11
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    12
object Term {
70383
38ac2e714729 more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents: 68265
diff changeset
    13
  /* types and terms */
38ac2e714729 more operations (avoid clones in Isabelle/MMT and Isabelle/Dedukti);
wenzelm
parents: 68265
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    15
  sealed case class Indexname(name: String, index: Int = 0) {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    16
    private lazy val hash: Int = (name, index).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    17
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    18
70537
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    19
    override def toString: String =
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    20
      if (index == -1) name
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    21
      else {
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    22
        val dot =
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    23
          Symbol.explode(name).reverse match {
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    24
            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
    25
            case c :: _ => Symbol.is_digit(c)
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    26
            case _ => true
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    27
          }
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    28
        if (dot) "?" + name + "." + index
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    29
        else if (index != 0) "?" + name + index
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    30
        else "?" + name
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    31
      }
17160e0a60b6 Indexname.toString according to string_of_vname' in ML;
wenzelm
parents: 70536
diff changeset
    32
  }
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    33
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
    34
  type Class = String
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
    35
  type Sort = List[Class]
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    36
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    37
  sealed abstract class Typ
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    38
  case class Type(name: String, args: List[Typ] = Nil) extends Typ {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    39
    private lazy val hash: Int = ("Type", name, args).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    40
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    41
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    42
    override def toString: String =
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    43
      if (this == dummyT) "_"
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    44
      else "Type(" + name + (if (args.isEmpty) "" else "," + args) + ")"
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    45
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    46
  case class TFree(name: String, sort: Sort = Nil) extends Typ {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    47
    private lazy val hash: Int = ("TFree", name, sort).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    48
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    49
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    50
    override def toString: String =
70851
6d01eca49b34 clarified signature default;
wenzelm
parents: 70850
diff changeset
    51
      "TFree(" + name + (if (sort.isEmpty) "" else "," + sort) + ")"
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    52
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    53
  case class TVar(name: Indexname, sort: Sort = Nil) extends Typ {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    54
    private lazy val hash: Int = ("TVar", name, sort).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    55
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    56
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    57
    override def toString: String =
70851
6d01eca49b34 clarified signature default;
wenzelm
parents: 70850
diff changeset
    58
      "TVar(" + name + (if (sort.isEmpty) "" else "," + sort) + ")"
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    59
  }
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71221
diff changeset
    60
  val dummyT: Type = Type("dummy")
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    61
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    62
  sealed abstract class Term {
71221
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    63
    def head: Term =
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    64
      this match {
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    65
        case App(fun, _) => fun.head
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    66
        case _ => this
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    67
      }
4dfb7c937126 more operations;
wenzelm
parents: 70916
diff changeset
    68
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    69
  case class Const(name: String, typargs: List[Typ] = Nil) extends Term {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    70
    private lazy val hash: Int = ("Const", name, typargs).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    71
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    72
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    73
    override def toString: String =
70897
2cc7c05b3b3c support dummy term;
wenzelm
parents: 70852
diff changeset
    74
      if (this == dummy) "_"
2cc7c05b3b3c support dummy term;
wenzelm
parents: 70852
diff changeset
    75
      else "Const(" + name + (if (typargs.isEmpty) "" else "," + typargs) + ")"
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    76
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    77
  case class Free(name: String, typ: Typ = dummyT) extends Term {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    78
    private lazy val hash: Int = ("Free", name, typ).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    79
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    80
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    81
    override def toString: String =
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    82
      "Free(" + name + (if (typ == dummyT) "" else "," + typ) + ")"
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    83
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    84
  case class Var(name: Indexname, typ: Typ = dummyT) extends Term {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    85
    private lazy val hash: Int = ("Var", name, typ).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    86
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    87
70846
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    88
    override def toString: String =
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    89
      "Var(" + name + (if (typ == dummyT) "" else "," + typ) + ")"
3777d9aaaaad clarified output and input of Typ/Term;
wenzelm
parents: 70834
diff changeset
    90
  }
43730
a0ed7bc688b5 lambda terms with XML data representation in Scala;
wenzelm
parents:
diff changeset
    91
  case class Bound(index: Int) extends Term
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    92
  case class Abs(name: String, typ: Typ, body: Term) extends Term {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    93
    private lazy val hash: Int = ("Abs", name, typ, body).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    94
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    95
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
    96
  case class App(fun: Term, arg: Term) extends Term {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    97
    private lazy val hash: Int = ("App", fun, arg).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    98
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
    99
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   100
    override def toString: String =
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   101
      this match {
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   102
        case OFCLASS(ty, c) => "OFCLASS(" + ty + "," + c + ")"
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   103
        case _ => "App(" + fun + "," + arg + ")"
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   104
      }
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   105
  }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   106
70897
2cc7c05b3b3c support dummy term;
wenzelm
parents: 70852
diff changeset
   107
  def dummy_pattern(ty: Typ): Term = Const("Pure.dummy_pattern", List(ty))
2cc7c05b3b3c support dummy term;
wenzelm
parents: 70852
diff changeset
   108
  val dummy: Term = dummy_pattern(dummyT)
2cc7c05b3b3c support dummy term;
wenzelm
parents: 70852
diff changeset
   109
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   110
  sealed abstract class Proof
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   111
  case object MinProof extends Proof
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   112
  case class PBound(index: Int) extends Proof
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   113
  case class Abst(name: String, typ: Typ, body: Proof) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   114
    private lazy val hash: Int = ("Abst", name, typ, body).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   115
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   116
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   117
  case class AbsP(name: String, hyp: Term, body: Proof) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   118
    private lazy val hash: Int = ("AbsP", name, hyp, body).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   119
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   120
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   121
  case class Appt(fun: Proof, arg: Term) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   122
    private lazy val hash: Int = ("Appt", fun, arg).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   123
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   124
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   125
  case class AppP(fun: Proof, arg: Proof) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   126
    private lazy val hash: Int = ("AppP", fun, arg).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   127
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   128
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   129
  case class Hyp(hyp: Term) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   130
    private lazy val hash: Int = ("Hyp", hyp).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   131
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   132
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   133
  case class PAxm(name: String, types: List[Typ]) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   134
    private lazy val hash: Int = ("PAxm", name, types).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   135
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   136
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   137
  case class PClass(typ: Typ, cls: Class) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   138
    private lazy val hash: Int = ("PClass", typ, cls).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   139
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   140
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   141
  case class Oracle(name: String, prop: Term, types: List[Typ]) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   142
    private lazy val hash: Int = ("Oracle", name, prop, types).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   143
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   144
  }
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   145
  case class PThm(serial: Long, theory_name: String, name: String, types: List[Typ]) extends Proof {
74121
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   146
    private lazy val hash: Int = ("PThm", serial, theory_name, name, types).hashCode()
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   147
    override def hashCode(): Int = hash
bc03b0b82fe6 prefer persistent hash code for cachable items (see also 72b13af7f266);
wenzelm
parents: 73031
diff changeset
   148
  }
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   149
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   150
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   151
  /* type classes within the logic */
70385
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   152
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   153
  object Class_Const {
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   154
    val suffix = "_class"
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   155
    def apply(c: Class): String = c + suffix
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   156
    def unapply(s: String): Option[Class] =
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   157
      if (s.endsWith(suffix)) Some(s.substring(0, s.length - suffix.length)) else None
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   158
  }
70385
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   159
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   160
  object OFCLASS {
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   161
    def apply(ty: Typ, s: Sort): List[Term] = s.map(c => apply(ty, c))
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   162
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   163
    def apply(ty: Typ, c: Class): Term =
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   164
      App(Const(Class_Const(c), List(ty)), Const(Pure_Thy.TYPE, List(ty)))
70385
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   165
70850
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   166
    def unapply(t: Term): Option[(Typ, String)] =
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   167
      t match {
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   168
        case App(Const(Class_Const(c), List(ty)), Const(Pure_Thy.TYPE, List(ty1)))
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   169
        if ty == ty1 => Some((ty, c))
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   170
        case _ => None
006214c581ee more operations for type classes;
wenzelm
parents: 70846
diff changeset
   171
      }
70385
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   172
  }
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   173
68d2c533db9c more operations: support type classes within the logic;
wenzelm
parents: 70383
diff changeset
   174
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   175
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   176
  /** cache **/
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   177
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   178
  object Cache {
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   179
    def make(
74731
161e84e6b40a just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents: 74121
diff changeset
   180
        xz: XZ.Cache = XZ.Cache.make(),
73026
237bd6318cc1 more uniform default --- hardly relevant in practice;
wenzelm
parents: 73024
diff changeset
   181
        max_string: Int = isabelle.Cache.default_max_string,
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   182
        initial_size: Int = isabelle.Cache.default_initial_size): Cache =
74731
161e84e6b40a just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents: 74121
diff changeset
   183
      new Cache(xz, initial_size, max_string)
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   184
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   185
    val none: Cache = make(max_string = 0)
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   186
  }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   187
74731
161e84e6b40a just one cache, via HTML_Context, via Sessions.Store or Session;
wenzelm
parents: 74121
diff changeset
   188
  class Cache(xz: XZ.Cache, max_string: Int, initial_size: Int)
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   189
  extends XML.Cache(xz, max_string, initial_size) {
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   190
    protected def cache_indexname(x: Indexname): Indexname =
70536
fe4d545f12e3 clarified type Indexname, with plain value Int;
wenzelm
parents: 70535
diff changeset
   191
      lookup(x) getOrElse store(Indexname(cache_string(x.name), x.index))
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   192
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   193
    protected def cache_sort(x: Sort): Sort =
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   194
      if (x.isEmpty) Nil
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   195
      else lookup(x) getOrElse store(x.map(cache_string))
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   196
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   197
    protected def cache_typ(x: Typ): Typ = {
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   198
      if (x == dummyT) dummyT
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   199
      else
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   200
        lookup(x) match {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   201
          case Some(y) => y
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   202
          case None =>
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   203
            x match {
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   204
              case Type(name, args) => store(Type(cache_string(name), cache_typs(args)))
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   205
              case TFree(name, sort) => store(TFree(cache_string(name), cache_sort(sort)))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   206
              case TVar(name, sort) => store(TVar(cache_indexname(name), cache_sort(sort)))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   207
            }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   208
        }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   209
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   210
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   211
    protected def cache_typs(x: List[Typ]): List[Typ] = {
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   212
      if (x.isEmpty) Nil
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   213
      else {
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   214
        lookup(x) match {
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   215
          case Some(y) => y
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 71221
diff changeset
   216
          case None => store(x.map(cache_typ))
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   217
        }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   218
      }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   219
    }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   220
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   221
    protected def cache_term(x: Term): Term = {
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   222
      lookup(x) match {
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   223
        case Some(y) => y
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   224
        case None =>
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   225
          x match {
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 70559
diff changeset
   226
            case Const(name, typargs) => store(Const(cache_string(name), cache_typs(typargs)))
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   227
            case Free(name, typ) => store(Free(cache_string(name), cache_typ(typ)))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   228
            case Var(name, typ) => store(Var(cache_indexname(name), cache_typ(typ)))
70536
fe4d545f12e3 clarified type Indexname, with plain value Int;
wenzelm
parents: 70535
diff changeset
   229
            case Bound(_) => store(x)
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   230
            case Abs(name, typ, body) =>
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   231
              store(Abs(cache_string(name), cache_typ(typ), cache_term(body)))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   232
            case App(fun, arg) => store(App(cache_term(fun), cache_term(arg)))
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   233
          }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   234
      }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   235
    }
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   236
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74731
diff changeset
   237
    protected def cache_proof(x: Proof): Proof = {
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   238
      if (x == MinProof) MinProof
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   239
      else {
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   240
        lookup(x) match {
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   241
          case Some(y) => y
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   242
          case None =>
71781
3fd54f7f52b0 tuned signature -- avoid warnings;
wenzelm
parents: 71777
diff changeset
   243
            (x: @unchecked) match {
70536
fe4d545f12e3 clarified type Indexname, with plain value Int;
wenzelm
parents: 70535
diff changeset
   244
              case PBound(_) => store(x)
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   245
              case Abst(name, typ, body) =>
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   246
                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
   247
              case AbsP(name, hyp, body) =>
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   248
                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
   249
              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
   250
              case AppP(fun, arg) => store(AppP(cache_proof(fun), cache_proof(arg)))
70535
de6062ac70b6 more complete pattern match;
wenzelm
parents: 70533
diff changeset
   251
              case Hyp(hyp) => store(Hyp(cache_term(hyp)))
de6062ac70b6 more complete pattern match;
wenzelm
parents: 70533
diff changeset
   252
              case PAxm(name, types) => store(PAxm(cache_string(name), cache_typs(types)))
71777
3875815f5967 clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents: 71601
diff changeset
   253
              case PClass(typ, cls) => store(PClass(cache_typ(typ), cache_string(cls)))
70533
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   254
              case Oracle(name, prop, types) =>
70834
614ca81fa28e clarified oracle_proof;
wenzelm
parents: 70786
diff changeset
   255
                store(Oracle(cache_string(name), cache_term(prop), cache_typs(types)))
70538
fc9ba6fe367f clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents: 70537
diff changeset
   256
              case PThm(serial, theory_name, name, types) =>
fc9ba6fe367f clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents: 70537
diff changeset
   257
                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
   258
            }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   259
        }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   260
      }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   261
    }
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 70385
diff changeset
   262
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   263
    // main methods
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   264
    def indexname(x: Indexname): Indexname =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   265
      if (no_cache) x else synchronized { cache_indexname(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   266
    def sort(x: Sort): Sort =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   267
      if (no_cache) x else synchronized { cache_sort(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   268
    def typ(x: Typ): Typ =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   269
      if (no_cache) x else synchronized { cache_typ(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   270
    def term(x: Term): Term =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   271
      if (no_cache) x else synchronized { cache_term(x) }
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   272
    def proof(x: Proof): Proof =
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   273
      if (no_cache) x else synchronized { cache_proof(x) }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   274
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   275
    def position(x: Position.T): Position.T =
73024
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   276
      if (no_cache) x
337e1b135d2f clarified signature --- internal Cache.none;
wenzelm
parents: 71781
diff changeset
   277
      else synchronized { x.map({ case (a, b) => (cache_string(a), cache_string(b)) }) }
68265
f0899dad4877 more general cache, also for term substructures;
wenzelm
parents: 43779
diff changeset
   278
  }
43731
70072780e095 inner syntax supports inlined YXML according to Term_XML (particularly useful for producing text under program control);
wenzelm
parents: 43730
diff changeset
   279
}