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;
wenzelm@43730
     1
/*  Title:      Pure/term.scala
wenzelm@43730
     2
    Author:     Makarius
wenzelm@43730
     3
wenzelm@43730
     4
Lambda terms with XML data representation.
wenzelm@43730
     5
wenzelm@43730
     6
Note: Isabelle/ML is the primary environment for logical operations.
wenzelm@43730
     7
*/
wenzelm@43730
     8
wenzelm@43730
     9
package isabelle
wenzelm@43730
    10
wenzelm@43730
    11
wenzelm@43730
    12
object Term
wenzelm@43730
    13
{
wenzelm@43730
    14
  /* basic type definitions */
wenzelm@43730
    15
wenzelm@43730
    16
  type Indexname = (String, Int)
wenzelm@43730
    17
wenzelm@43730
    18
  type Sort = List[String]
wenzelm@43730
    19
  val dummyS: Sort = List("")
wenzelm@43730
    20
wenzelm@43730
    21
  sealed abstract class Typ
wenzelm@43730
    22
  case class Type(name: String, args: List[Typ] = Nil) extends Typ
wenzelm@43730
    23
  case class TFree(name: String, sort: Sort = dummyS) extends Typ
wenzelm@43730
    24
  case class TVar(name: Indexname, sort: Sort = dummyS) extends Typ
wenzelm@43730
    25
  val dummyT = Type("dummy")
wenzelm@43730
    26
wenzelm@43730
    27
  sealed abstract class Term
wenzelm@43730
    28
  case class Const(name: String, typ: Typ = dummyT) extends Term
wenzelm@43730
    29
  case class Free(name: String, typ: Typ = dummyT) extends Term
wenzelm@43730
    30
  case class Var(name: Indexname, typ: Typ = dummyT) extends Term
wenzelm@43730
    31
  case class Bound(index: Int) extends Term
wenzelm@43730
    32
  case class Abs(name: String, typ: Typ = dummyT, body: Term) extends Term
wenzelm@43730
    33
  case class App(fun: Term, arg: Term) extends Term
wenzelm@43731
    34
}
wenzelm@43731
    35
wenzelm@43731
    36
wenzelm@43731
    37
object Term_XML
wenzelm@43731
    38
{
wenzelm@43731
    39
  import Term._
wenzelm@43730
    40
wenzelm@43730
    41
wenzelm@43730
    42
  /* XML data representation */
wenzelm@43730
    43
wenzelm@43731
    44
  object Encode
wenzelm@43730
    45
  {
wenzelm@43767
    46
    import XML.Encode._
wenzelm@43730
    47
wenzelm@43731
    48
    val indexname: T[Indexname] = pair(string, int)
wenzelm@43730
    49
wenzelm@43731
    50
    val sort: T[Sort] = list(string)
wenzelm@43730
    51
wenzelm@43731
    52
    def typ: T[Typ] =
wenzelm@43731
    53
      variant[Typ](List(
wenzelm@43731
    54
        { case Type(a, b) => pair(string, list(typ))((a, b)) },
wenzelm@43731
    55
        { case TFree(a, b) => pair(string, sort)((a, b)) },
wenzelm@43731
    56
        { case TVar(a, b) => pair(indexname, sort)((a, b)) }))
wenzelm@43730
    57
wenzelm@43731
    58
    def term: T[Term] =
wenzelm@43731
    59
      variant[Term](List(
wenzelm@43731
    60
        { case Const(a, b) => pair(string, typ)((a, b)) },
wenzelm@43731
    61
        { case Free(a, b) => pair(string, typ)((a, b)) },
wenzelm@43731
    62
        { case Var(a, b) => pair(indexname, typ)((a, b)) },
wenzelm@43731
    63
        { case Bound(a) => int(a) },
wenzelm@43731
    64
        { case Abs(a, b, c) => triple(string, typ, term)((a, b, c)) },
wenzelm@43731
    65
        { case App(a, b) => pair(term, term)((a, b)) }))
wenzelm@43731
    66
  }
wenzelm@43730
    67
wenzelm@43731
    68
  object Decode
wenzelm@43731
    69
  {
wenzelm@43767
    70
    import XML.Decode._
wenzelm@43730
    71
wenzelm@43731
    72
    val indexname: T[Indexname] = pair(string, int)
wenzelm@43730
    73
wenzelm@43731
    74
    val sort: T[Sort] = list(string)
wenzelm@43730
    75
wenzelm@43731
    76
    def typ: T[Typ] =
wenzelm@43731
    77
      variant[Typ](List(
wenzelm@43731
    78
        (x => { val (a, b) = pair(string, list(typ))(x); Type(a, b) }),
wenzelm@43731
    79
        (x => { val (a, b) = pair(string, sort)(x); TFree(a, b) }),
wenzelm@43731
    80
        (x => { val (a, b) = pair(indexname, sort)(x); TVar(a, b) })))
wenzelm@43730
    81
wenzelm@43731
    82
    def term: T[Term] =
wenzelm@43731
    83
      variant[Term](List(
wenzelm@43731
    84
        (x => { val (a, b) = pair(string, typ)(x); Const(a, b) }),
wenzelm@43731
    85
        (x => { val (a, b) = pair(string, typ)(x); Free(a, b) }),
wenzelm@43731
    86
        (x => { val (a, b) = pair(indexname, typ)(x); Var(a, b) }),
wenzelm@43731
    87
        (x => Bound(int(x))),
wenzelm@43731
    88
        (x => { val (a, b, c) = triple(string, typ, term)(x); Abs(a, b, c) }),
wenzelm@43731
    89
        (x => { val (a, b) = pair(term, term)(x); App(a, b) })))
wenzelm@43730
    90
  }
wenzelm@43730
    91
}