src/Pure/term_xml.scala
author wenzelm
Thu, 10 Oct 2019 16:51:47 +0200
changeset 70828 cb70d84a9f5e
parent 70784 799437173553
child 70834 614ca81fa28e
permissions -rw-r--r--
more compact XML representation;
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
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    24
    def typ_raw: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    25
      variant[Typ](List(
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    26
        { case Type(a, b) => (List(a), list(typ_raw)(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
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    30
    val typ: T[Typ] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    31
      { case t => option(typ_raw)(if (t == dummyT) None else Some(t)) }
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    32
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    33
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    34
      variant[Term](List(
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 70559
diff changeset
    35
        { case Const(a, b) => (List(a), list(typ)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    36
        { case Free(a, b) => (List(a), typ(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    37
        { case Var(a, b) => (indexname(a), typ(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    38
        { case Bound(a) => (List(int_atom(a)), Nil) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    39
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    40
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
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
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    43
  object Decode
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    44
  {
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    45
    import XML.Decode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    46
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    47
    val indexname: P[Indexname] =
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    48
      { case List(a) => Indexname(a, 0)
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    49
        case List(a, b) => Indexname(a, int_atom(b)) }
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    50
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    51
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    52
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    53
    def typ_raw: T[Typ] =
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    54
      variant[Typ](List(
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    55
        { case (List(a), b) => Type(a, list(typ_raw)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    56
        { case (List(a), b) => TFree(a, sort(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    57
        { case (a, b) => TVar(indexname(a), sort(b)) }))
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    58
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    59
    def typ(body: XML.Body): Typ = option(typ_raw)(body).getOrElse(dummyT)
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    60
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    61
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    62
      variant[Term](List(
70784
799437173553 Term_XML.Encode/Decode.term uses Const "typargs";
wenzelm
parents: 70559
diff changeset
    63
        { case (List(a), b) => Const(a, list(typ)(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    64
        { case (List(a), b) => Free(a, typ(b)) },
70828
cb70d84a9f5e more compact XML representation;
wenzelm
parents: 70784
diff changeset
    65
        { case (a, b) => Var(indexname(a), typ(b)) },
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    66
        { case (List(a), Nil) => Bound(int_atom(a)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    67
        { 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
    68
        { 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
    69
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    70
    def proof: T[Proof] =
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    71
      variant[Proof](List(
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    72
        { case (Nil, Nil) => MinProof },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    73
        { case (List(a), Nil) => PBound(int_atom(a)) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    74
        { case (List(a), b) => val (c, d) = pair(typ, proof)(b); Abst(a, c, d) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    75
        { case (List(a), b) => val (c, d) = pair(term, proof)(b); AbsP(a, c, d) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    76
        { case (Nil, a) => val (b, c) = pair(proof, term)(a); Appt(b, c) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    77
        { case (Nil, a) => val (b, c) = pair(proof, proof)(a); AppP(b, c) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    78
        { case (Nil, a) => Hyp(term(a)) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    79
        { case (List(a), b) => PAxm(a, list(typ)(b)) },
031620901fcd support for (fully reconstructed) proof terms in Scala;
wenzelm
parents: 43779
diff changeset
    80
        { case (List(a), b) => OfClass(typ(b), a) },
70559
c92443e8d724 clarified type for recorded oracles;
wenzelm
parents: 70538
diff changeset
    81
        { case (List(a), b) => val (c, d) = pair(option(term), list(typ))(b); Oracle(a, c, d) },
70538
fc9ba6fe367f clarified PThm: theory_name simplifies retrieval from exports;
wenzelm
parents: 70536
diff changeset
    82
        { case (List(a, b, c), d) => PThm(long_atom(a), b, c, list(typ)(d)) }))
43779
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    83
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    84
}