src/Pure/term_xml.scala
author wenzelm
Mon Dec 04 22:54:31 2017 +0100 (20 months ago)
changeset 67131 85d10959c2e4
parent 43779 47bec02c6762
child 70533 031620901fcd
permissions -rw-r--r--
tuned signature;
wenzelm@43779
     1
/*  Title:      Pure/term_xml.scala
wenzelm@43779
     2
    Author:     Makarius
wenzelm@43779
     3
wenzelm@43779
     4
XML data representation of lambda terms.
wenzelm@43779
     5
*/
wenzelm@43779
     6
wenzelm@43779
     7
package isabelle
wenzelm@43779
     8
wenzelm@43779
     9
wenzelm@43779
    10
object Term_XML
wenzelm@43779
    11
{
wenzelm@43779
    12
  import Term._
wenzelm@43779
    13
wenzelm@43779
    14
  object Encode
wenzelm@43779
    15
  {
wenzelm@43779
    16
    import XML.Encode._
wenzelm@43779
    17
wenzelm@43779
    18
    val sort: T[Sort] = list(string)
wenzelm@43779
    19
wenzelm@43779
    20
    def typ: T[Typ] =
wenzelm@43779
    21
      variant[Typ](List(
wenzelm@43779
    22
        { case Type(a, b) => (List(a), list(typ)(b)) },
wenzelm@43779
    23
        { case TFree(a, b) => (List(a), sort(b)) },
wenzelm@43779
    24
        { case TVar((a, b), c) => (List(a, int_atom(b)), sort(c)) }))
wenzelm@43779
    25
wenzelm@43779
    26
    def term: T[Term] =
wenzelm@43779
    27
      variant[Term](List(
wenzelm@43779
    28
        { case Const(a, b) => (List(a), typ(b)) },
wenzelm@43779
    29
        { case Free(a, b) => (List(a), typ(b)) },
wenzelm@43779
    30
        { case Var((a, b), c) => (List(a, int_atom(b)), typ(c)) },
wenzelm@43779
    31
        { case Bound(a) => (List(int_atom(a)), Nil) },
wenzelm@43779
    32
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
wenzelm@43779
    33
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
wenzelm@43779
    34
  }
wenzelm@43779
    35
wenzelm@43779
    36
  object Decode
wenzelm@43779
    37
  {
wenzelm@43779
    38
    import XML.Decode._
wenzelm@43779
    39
wenzelm@43779
    40
    val sort: T[Sort] = list(string)
wenzelm@43779
    41
wenzelm@43779
    42
    def typ: T[Typ] =
wenzelm@43779
    43
      variant[Typ](List(
wenzelm@43779
    44
        { case (List(a), b) => Type(a, list(typ)(b)) },
wenzelm@43779
    45
        { case (List(a), b) => TFree(a, sort(b)) },
wenzelm@43779
    46
        { case (List(a, b), c) => TVar((a, int_atom(b)), sort(c)) }))
wenzelm@43779
    47
wenzelm@43779
    48
    def term: T[Term] =
wenzelm@43779
    49
      variant[Term](List(
wenzelm@43779
    50
        { case (List(a), b) => Const(a, typ(b)) },
wenzelm@43779
    51
        { case (List(a), b) => Free(a, typ(b)) },
wenzelm@43779
    52
        { case (List(a, b), c) => Var((a, int_atom(b)), typ(c)) },
wenzelm@43779
    53
        { case (List(a), Nil) => Bound(int_atom(a)) },
wenzelm@43779
    54
        { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
wenzelm@43779
    55
        { case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
wenzelm@43779
    56
  }
wenzelm@43779
    57
}