src/Pure/term.scala
author wenzelm
Tue Jul 12 10:44:30 2011 +0200 (2011-07-12)
changeset 43767 e0219ef7f84c
parent 43731 70072780e095
child 43778 ce9189450447
permissions -rw-r--r--
tuned XML modules;
     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 indexname: T[Indexname] = pair(string, int)
    49 
    50     val sort: T[Sort] = list(string)
    51 
    52     def typ: T[Typ] =
    53       variant[Typ](List(
    54         { case Type(a, b) => pair(string, list(typ))((a, b)) },
    55         { case TFree(a, b) => pair(string, sort)((a, b)) },
    56         { case TVar(a, b) => pair(indexname, sort)((a, b)) }))
    57 
    58     def term: T[Term] =
    59       variant[Term](List(
    60         { case Const(a, b) => pair(string, typ)((a, b)) },
    61         { case Free(a, b) => pair(string, typ)((a, b)) },
    62         { case Var(a, b) => pair(indexname, typ)((a, b)) },
    63         { case Bound(a) => int(a) },
    64         { case Abs(a, b, c) => triple(string, typ, term)((a, b, c)) },
    65         { case App(a, b) => pair(term, term)((a, b)) }))
    66   }
    67 
    68   object Decode
    69   {
    70     import XML.Decode._
    71 
    72     val indexname: T[Indexname] = pair(string, int)
    73 
    74     val sort: T[Sort] = list(string)
    75 
    76     def typ: T[Typ] =
    77       variant[Typ](List(
    78         (x => { val (a, b) = pair(string, list(typ))(x); Type(a, b) }),
    79         (x => { val (a, b) = pair(string, sort)(x); TFree(a, b) }),
    80         (x => { val (a, b) = pair(indexname, sort)(x); TVar(a, b) })))
    81 
    82     def term: T[Term] =
    83       variant[Term](List(
    84         (x => { val (a, b) = pair(string, typ)(x); Const(a, b) }),
    85         (x => { val (a, b) = pair(string, typ)(x); Free(a, b) }),
    86         (x => { val (a, b) = pair(indexname, typ)(x); Var(a, b) }),
    87         (x => Bound(int(x))),
    88         (x => { val (a, b, c) = triple(string, typ, term)(x); Abs(a, b, c) }),
    89         (x => { val (a, b) = pair(term, term)(x); App(a, b) })))
    90   }
    91 }