src/Pure/term_xml.scala
author wenzelm
Wed, 31 Jul 2019 19:50:38 +0200
changeset 70451 550a5a822edb
parent 43779 47bec02c6762
child 70533 031620901fcd
permissions -rw-r--r--
clarified export: retain proof boxes as local definitions -- more scalable;
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
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    18
    val sort: T[Sort] = list(string)
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    19
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    20
    def typ: T[Typ] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    21
      variant[Typ](List(
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    22
        { case Type(a, b) => (List(a), list(typ)(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    23
        { case TFree(a, b) => (List(a), sort(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    24
        { case TVar((a, b), c) => (List(a, int_atom(b)), sort(c)) }))
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    25
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    26
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    27
      variant[Term](List(
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    28
        { case Const(a, b) => (List(a), typ(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    29
        { case Free(a, b) => (List(a), typ(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    30
        { case Var((a, b), c) => (List(a, int_atom(b)), typ(c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    31
        { case Bound(a) => (List(int_atom(a)), Nil) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    32
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    33
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    34
  }
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    35
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    36
  object Decode
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    37
  {
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    38
    import XML.Decode._
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    39
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    40
    val sort: T[Sort] = list(string)
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
    def typ: T[Typ] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    43
      variant[Typ](List(
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    44
        { case (List(a), b) => Type(a, list(typ)(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    45
        { case (List(a), b) => TFree(a, sort(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    46
        { case (List(a, b), c) => TVar((a, int_atom(b)), sort(c)) }))
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    47
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    48
    def term: T[Term] =
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    49
      variant[Term](List(
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    50
        { case (List(a), b) => Const(a, typ(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    51
        { case (List(a), b) => Free(a, typ(b)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    52
        { case (List(a, b), c) => Var((a, int_atom(b)), typ(c)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    53
        { case (List(a), Nil) => Bound(int_atom(a)) },
47bec02c6762 more uniform Term and Term_XML modules;
wenzelm
parents:
diff changeset
    54
        { 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
    55
        { case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
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
}