src/Pure/term_xml.scala
author wenzelm
Sun, 19 May 2024 18:43:45 +0200
changeset 80181 aa92c0f96036
parent 75436 40630fec3b5d
child 80295 8a9588ffc133
permissions -rw-r--r--
provide scala-3.4.2, but do not activate it: scala-3.3.x is LTS version;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/term_xml.scala
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     3
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     4
XML data representation of lambda terms.
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     5
*/
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     6
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     7
package isabelle
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     8
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
     9
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71777
diff changeset
    10
object Term_XML {
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    11
  import Term._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    12
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71777
diff changeset
    13
  object Encode {
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    14
    import XML.Encode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    15
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    16
    val indexname: P[Indexname] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    17
      { case Indexname(a, 0) => List(a)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    18
        case Indexname(a, b) => List(a, int_atom(b)) }
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    19
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    20
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    21
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    22
    def typ: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    23
      variant[Typ](List(
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    24
        { case Type(a, b) => (List(a), list(typ)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    25
        { case TFree(a, b) => (List(a), sort(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    26
        { case TVar(a, b) => (indexname(a), sort(b)) }))
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    27
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    28
    val typ_body: T[Typ] = (t: Typ) => if (t == dummyT) Nil else typ(t)
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    29
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    30
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    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
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    33
        { case Free(a, b) => (List(a), typ_body(b)) },
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    34
        { case Var(a, b) => (indexname(a), typ_body(b)) },
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70843
diff changeset
    35
        { case Bound(a) => (Nil, int(a)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    36
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    37
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    38
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    39
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71777
diff changeset
    40
  object Decode {
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    41
    import XML.Decode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    42
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    43
    val indexname: P[Indexname] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    44
      { case List(a) => Indexname(a, 0)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    45
        case List(a, b) => Indexname(a, int_atom(b)) }
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    46
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    47
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    48
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    49
    def typ: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    50
      variant[Typ](List(
75436
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    51
        { case (List(a), b) => Type(a, list(typ)(b)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    52
        { case (List(a), b) => TFree(a, sort(b)) },
75427
323481d143c6 tuned --- avoid warnings in scala3;
wenzelm
parents: 75425
diff changeset
    53
        { case (a, b) => TVar(indexname(a), sort(b)) }))
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    54
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    55
    val typ_body: T[Typ] = { case Nil => dummyT case body => typ(body) }
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    56
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    57
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    58
      variant[Term](List(
75436
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    59
        { case (List(a), b) => Const(a, list(typ)(b)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    60
        { case (List(a), b) => Free(a, typ_body(b)) },
75427
323481d143c6 tuned --- avoid warnings in scala3;
wenzelm
parents: 75425
diff changeset
    61
        { case (a, b) => Var(indexname(a), typ_body(b)) },
75436
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    62
        { case (Nil, a) => Bound(int(a)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    63
        { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71777
diff changeset
    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
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    72
          { case (List(a), b) => Const(a, list(typ)(b)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    73
          { case (List(a), b) => Free(a, env_type(a, typ_body(b))) },
75427
323481d143c6 tuned --- avoid warnings in scala3;
wenzelm
parents: 75425
diff changeset
    74
          { case (a, b) => Var(indexname(a), typ_body(b)) },
75436
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    75
          { case (Nil, a) => Bound(int(a)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    76
          { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 71777
diff changeset
    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
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    85
          { case (Nil, Nil) => MinProof },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    86
          { case (Nil, a) => PBound(int(a)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    87
          { case (List(a), b) => val (c, d) = pair(typ, proof)(b); Abst(a, c, d) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    88
          { case (List(a), b) => val (c, d) = pair(term, proof)(b); AbsP(a, c, d) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    89
          { case (Nil, a) => val (b, c) = pair(proof, term)(a); Appt(b, c) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    90
          { case (Nil, a) => val (b, c) = pair(proof, proof)(a); AppP(b, c) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    91
          { case (Nil, a) => Hyp(term(a)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    92
          { case (List(a), b) => PAxm(a, list(typ)(b)) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    93
          { case (List(a), b) => PClass(typ(b), a) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    94
          { case (List(a), b) => val (c, d) = pair(term, list(typ))(b); Oracle(a, c, d) },
40630fec3b5d clarified signature;
wenzelm
parents: 75427
diff changeset
    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
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
   100
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
   101
}