src/Pure/term.scala
author wenzelm
Sun Jul 10 17:58:11 2011 +0200 (2011-07-10)
changeset 43730 a0ed7bc688b5
child 43731 70072780e095
permissions -rw-r--r--
lambda terms with XML data representation in Scala;
avoid `class` in signature;
     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   /* XML data representation */
    37 
    38   object XML
    39   {
    40     object Make
    41     {
    42       import XML_Data.Make._
    43 
    44       val indexname: T[Indexname] = pair(string, int)
    45 
    46       val sort: T[Sort] = list(string)
    47 
    48       def typ: T[Typ] =
    49         variant[Typ](List(
    50           { case Type(a, b) => pair(string, list(typ))((a, b)) },
    51           { case TFree(a, b) => pair(string, sort)((a, b)) },
    52           { case TVar(a, b) => pair(indexname, sort)((a, b)) }))
    53 
    54       def term: T[Term] =
    55         variant[Term](List(
    56           { case Const(a, b) => pair(string, typ)((a, b)) },
    57           { case Free(a, b) => pair(string, typ)((a, b)) },
    58           { case Var(a, b) => pair(indexname, typ)((a, b)) },
    59           { case Bound(a) => int(a) },
    60           { case Abs(a, b, c) => triple(string, typ, term)((a, b, c)) },
    61           { case App(a, b) => pair(term, term)((a, b)) }))
    62     }
    63 
    64     object Dest
    65     {
    66       import XML_Data.Dest._
    67 
    68       val indexname: T[Indexname] = pair(string, int)
    69 
    70       val sort: T[Sort] = list(string)
    71 
    72       def typ: T[Typ] =
    73         variant[Typ](List(
    74           (x => { val (a, b) = pair(string, list(typ))(x); Type(a, b) }),
    75           (x => { val (a, b) = pair(string, sort)(x); TFree(a, b) }),
    76           (x => { val (a, b) = pair(indexname, sort)(x); TVar(a, b) })))
    77 
    78       def term: T[Term] =
    79         variant[Term](List(
    80           (x => { val (a, b) = pair(string, typ)(x); Const(a, b) }),
    81           (x => { val (a, b) = pair(string, typ)(x); Free(a, b) }),
    82           (x => { val (a, b) = pair(indexname, typ)(x); Var(a, b) }),
    83           (x => Bound(int(x))),
    84           (x => { val (a, b, c) = triple(string, typ, term)(x); Abs(a, b, c) }),
    85           (x => { val (a, b) = pair(term, term)(x); App(a, b) })))
    86     }
    87   }
    88 }