src/Pure/term_xml.scala
author nipkow
Tue, 28 Apr 2020 22:55:51 +0200
changeset 71811 fa4f8f3d69f2
parent 71777 3875815f5967
child 75393 87ebf5a50283
permissions -rw-r--r--
merged
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
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    10
object Term_XML
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    11
{
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    12
  import Term._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    13
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    14
  object Encode
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    15
  {
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    16
    import XML.Encode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    17
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    18
    val indexname: P[Indexname] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    19
      { case Indexname(a, 0) => List(a)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    20
        case Indexname(a, b) => List(a, int_atom(b)) }
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    21
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    22
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    23
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    24
    def typ: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    25
      variant[Typ](List(
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    26
        { case Type(a, b) => (List(a), list(typ)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    27
        { case TFree(a, b) => (List(a), sort(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    28
        { case TVar(a, b) => (indexname(a), sort(b)) }))
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    29
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    30
    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
    31
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    32
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    33
      variant[Term](List(
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 70559
diff changeset
    34
        { case Const(a, b) => (List(a), list(typ)(b)) },
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    35
        { case Free(a, b) => (List(a), typ_body(b)) },
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    36
        { case Var(a, b) => (indexname(a), typ_body(b)) },
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70843
diff changeset
    37
        { case Bound(a) => (Nil, int(a)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    38
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    39
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    40
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    41
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    42
  object Decode
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    43
  {
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    44
    import XML.Decode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    45
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    46
    val indexname: P[Indexname] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    47
      { case List(a) => Indexname(a, 0)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    48
        case List(a, b) => Indexname(a, int_atom(b)) }
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    49
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    50
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    51
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    52
    def typ: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    53
      variant[Typ](List(
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    54
        { case (List(a), b) => Type(a, list(typ)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    55
        { case (List(a), b) => TFree(a, sort(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    56
        { case (a, b) => TVar(indexname(a), sort(b)) }))
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    57
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    58
    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
    59
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    60
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    61
      variant[Term](List(
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 70559
diff changeset
    62
        { case (List(a), b) => Const(a, list(typ)(b)) },
70842
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    63
        { case (List(a), b) => Free(a, typ_body(b)) },
c5caf81aa523 more compact XML;
wenzelm
parents: 70834
diff changeset
    64
        { case (a, b) => Var(indexname(a), typ_body(b)) },
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70843
diff changeset
    65
        { case (Nil, a) => Bound(int(a)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    66
        { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    67
        { 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
    68
70843
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    69
    def term_env(env: Map[String, Typ]): T[Term] =
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    70
    {
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    71
      def env_type(x: String, t: Typ): Typ =
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    72
        if (t == dummyT && env.isDefinedAt(x)) env(x) else t
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    73
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    74
      def term: T[Term] =
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    75
        variant[Term](List(
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    76
          { case (List(a), b) => Const(a, list(typ)(b)) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    77
          { case (List(a), b) => Free(a, env_type(a, typ_body(b))) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    78
          { case (a, b) => Var(indexname(a), typ_body(b)) },
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70843
diff changeset
    79
          { case (Nil, a) => Bound(int(a)) },
70843
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    80
          { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    81
          { case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    82
      term
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    83
    }
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    84
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    85
    def proof_env(env: Map[String, Typ]): T[Proof] =
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    86
    {
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    87
      val term = term_env(env)
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    88
      def proof: T[Proof] =
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    89
        variant[Proof](List(
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    90
          { case (Nil, Nil) => MinProof },
70844
f95a85446a24 more compact XML;
wenzelm
parents: 70843
diff changeset
    91
          { case (Nil, a) => PBound(int(a)) },
70843
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    92
          { case (List(a), b) => val (c, d) = pair(typ, proof)(b); Abst(a, c, d) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    93
          { case (List(a), b) => val (c, d) = pair(term, proof)(b); AbsP(a, c, d) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    94
          { case (Nil, a) => val (b, c) = pair(proof, term)(a); Appt(b, c) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    95
          { case (Nil, a) => val (b, c) = pair(proof, proof)(a); AppP(b, c) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    96
          { case (Nil, a) => Hyp(term(a)) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    97
          { case (List(a), b) => PAxm(a, list(typ)(b)) },
71777
3875815f5967 clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents: 70844
diff changeset
    98
          { case (List(a), b) => PClass(typ(b), a) },
70843
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
    99
          { case (List(a), b) => val (c, d) = pair(term, list(typ))(b); Oracle(a, c, d) },
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
   100
          { case (List(a, b, c), d) => PThm(long_atom(a), b, c, list(typ)(d)) }))
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
   101
      proof
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
   102
    }
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
   103
cc987440d776 more compact XML: separate environment for free variables;
wenzelm
parents: 70842
diff changeset
   104
    val proof: T[Proof] = proof_env(Map.empty)
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
   105
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
   106
}