src/Pure/term.scala
author wenzelm
Tue Jul 12 17:53:06 2011 +0200 (2011-07-12)
changeset 43778 ce9189450447
parent 43767 e0219ef7f84c
child 43779 47bec02c6762
permissions -rw-r--r--
more compact representation of XML data (notably sort/typ/term), using properties as vector of atomic values;
     1 /*  Title:      Pure/term.scala
     2     Author:     Makarius
     3 
     4 Lambda terms with XML data representation.
     5 
     6 Note: Isabelle/ML is the primary environment for logical operations.
     7 */
     8 
     9 package isabelle
    10 
    11 
    12 object Term
    13 {
    14   /* basic type definitions */
    15 
    16   type Indexname = (String, Int)
    17 
    18   type Sort = List[String]
    19   val dummyS: Sort = List("")
    20 
    21   sealed abstract class Typ
    22   case class Type(name: String, args: List[Typ] = Nil) extends Typ
    23   case class TFree(name: String, sort: Sort = dummyS) extends Typ
    24   case class TVar(name: Indexname, sort: Sort = dummyS) extends Typ
    25   val dummyT = Type("dummy")
    26 
    27   sealed abstract class Term
    28   case class Const(name: String, typ: Typ = dummyT) extends Term
    29   case class Free(name: String, typ: Typ = dummyT) extends Term
    30   case class Var(name: Indexname, typ: Typ = dummyT) extends Term
    31   case class Bound(index: Int) extends Term
    32   case class Abs(name: String, typ: Typ = dummyT, body: Term) extends Term
    33   case class App(fun: Term, arg: Term) extends Term
    34 }
    35 
    36 
    37 object Term_XML
    38 {
    39   import Term._
    40 
    41 
    42   /* XML data representation */
    43 
    44   object Encode
    45   {
    46     import XML.Encode._
    47 
    48     val sort: T[Sort] = list(string)
    49 
    50     def typ: T[Typ] =
    51       variant[Typ](List(
    52         { case Type(a, b) => (List(a), list(typ)(b)) },
    53         { case TFree(a, b) => (List(a), sort(b)) },
    54         { case TVar((a, b), c) => (List(a, int_atom(b)), sort(c)) }))
    55 
    56     def term: T[Term] =
    57       variant[Term](List(
    58         { case Const(a, b) => (List(a), typ(b)) },
    59         { case Free(a, b) => (List(a), typ(b)) },
    60         { case Var((a, b), c) => (List(a, int_atom(b)), typ(c)) },
    61         { case Bound(a) => (List(int_atom(a)), Nil) },
    62         { case Abs(a, b, c) => (List(a), pair(typ, term)(b, c)) },
    63         { case App(a, b) => (Nil, pair(term, term)(a, b)) }))
    64   }
    65 
    66   object Decode
    67   {
    68     import XML.Decode._
    69 
    70     val sort: T[Sort] = list(string)
    71 
    72     def typ: T[Typ] =
    73       variant[Typ](List(
    74         { case (List(a), b) => Type(a, list(typ)(b)) },
    75         { case (List(a), b) => TFree(a, sort(b)) },
    76         { case (List(a, b), c) => TVar((a, int_atom(b)), sort(c)) }))
    77 
    78     def term: T[Term] =
    79       variant[Term](List(
    80         { case (List(a), b) => Const(a, typ(b)) },
    81         { case (List(a), b) => Free(a, typ(b)) },
    82         { case (List(a, b), c) => Var((a, int_atom(b)), typ(c)) },
    83         { case (List(a), Nil) => Bound(int_atom(a)) },
    84         { case (List(a), b) => val (c, d) = pair(typ, term)(b); Abs(a, c, d) },
    85         { case (Nil, a) => val (b, c) = pair(term, term)(a); App(b, c) }))
    86   }
    87 }