src/Pure/term_xml.scala
author paulson <lp15@cam.ac.uk>
Sun, 20 May 2018 18:37:34 +0100
changeset 68239 0764ee22a4d1
parent 43779 47bec02c6762
child 70533 031620901fcd
permissions -rw-r--r--
tidy up of Derivative

/*  Title:      Pure/term_xml.scala
    Author:     Makarius

XML data representation of lambda terms.
*/

package isabelle


object Term_XML
{
  import Term._

  object Encode
  {
    import XML.Encode._

    val sort: T[Sort] = list(string)

    def typ: T[Typ] =
      variant[Typ](List(
        { case Type(a, b) => (List(a), list(typ)(b)) },
        { case TFree(a, b) => (List(a), sort(b)) },
        { case TVar((a, b), c) => (List(a, int_atom(b)), sort(c)) }))

    def term: T[Term] =
      variant[Term](List(
        { case Const(a, b) => (List(a), typ(b)) },
        { case Free(a, b) => (List(a), typ(b)) },
        { case Var((a, b), c) => (List(a, int_atom(b)), typ(c)) },
        { case Bound(a) => (List(int_atom(a)), Nil) },
        { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
        { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
  }

  object Decode
  {
    import XML.Decode._

    val sort: T[Sort] = list(string)

    def typ: T[Typ] =
      variant[Typ](List(
        { case (List(a), b) => Type(a, list(typ)(b)) },
        { case (List(a), b) => TFree(a, sort(b)) },
        { case (List(a, b), c) => TVar((a, int_atom(b)), sort(c)) }))

    def term: T[Term] =
      variant[Term](List(
        { case (List(a), b) => Const(a, typ(b)) },
        { case (List(a), b) => Free(a, typ(b)) },
        { case (List(a, b), c) => Var((a, int_atom(b)), typ(c)) },
        { case (List(a), Nil) => Bound(int_atom(a)) },
        { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
        { case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
  }
}