edu.umass.cs.cs691f

Subst

trait Subst extends AnyRef

A substitution that binds Syms to arbitrary values.

Substitutions are an abstract data type. See the companion object, which has methods Subst.singleton and Subst.empty that you can use to create new substitutions.

A Subst is generic in the sense that it recurs into any tuple or case class (or any type that extends scala.Product). A value that extends HasSym is treated as a variable to be substituted:

val x = Sym.fresh("x")
val subst = Subst.singleton(x, 10)
assert(subst[Any](new HasSym { val sym = x }) == 10)
val ey = new HasSym{ val sym = Sym.fresh("y") }
assert(subst[Any](ey) == ey)

You can create a hierarchy of case classes and substitute into them:

sealed abstract class Exp
case class EInt(n: Int) extends Exp
case class EVar(sym: Sym) extends Exp with HasSym
case class EAdd(e1: Exp, e2: Exp) extends Exp

val subst = Subst.singleton(x, EInt(99))
assert(subst[Exp](EAdd(EVar(x), EInt(10))) == EAdd(EInt(99), EInt(10)))

The explicit type parameter to the substitution is important. Without an explicit type parameter, Scala will infer that the return type of the following expressions as EVar:

subst(EVar(x)) // returns EInt(99) with type EVar!

Here is another example, where the variable is in the type and we're substituting into an expression:

case class EFun(x: String, t: Typ, e: Exp) extends Exp
case class EId(x: String) extends Exp
case class EApp(e1: Exp, e2: Exp) extends Exp

sealed abstract class Typ
case class TInt() extends Typ
case class TArr(t1: Typ, t2: Typ) extends Typ
case class TVar(sym: Sym) extends Typ with HasSym

val subst = Subst.singleton(x, TInt())
assert(subst[Exp](EFun("a", TVar(x), EId("a"))) == EFun("a", TInt(), EId("a")))

It is possible for a term to use a type-variable in a context that is incompatible with a substitution. In the following example, the substitution binds x to an Exp, but the term uses x as a Typ:

val subst = Subst.singleton(x, EInt(34))
subst[Exp](EFun("a", TVar(x), EId("a"))) /// throws exception
Linear Supertypes
AnyRef, Any
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Subst
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Learn more about member selection
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def apply[A](v: A): A

    Applies the substitution to a value.

    Applies the substitution to a value.

    apply recurs into tuples, case classes, and all values that extend scala.Product. Values that extend HasSym are treated as variables to be substituted.

  2. abstract def asMap: Map[Sym, Any]

    For debugging, a Map that represents the bindings in the substitution.

  3. abstract def compose(that: Subst): Subst

    Composes two substitutions.

    Composes two substitutions.

    The following identity holds:

    s1.apply[A](s2.apply[A](v)) == s1.compose(s2).apply[A](v)
  4. abstract def lookup(x: Sym): Option[Any]

    Lookup the value bound to x in the substitution.

Concrete Value Members

  1. final def !=(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  2. final def !=(arg0: Any): Boolean

    Definition Classes
    Any
  3. final def ##(): Int

    Definition Classes
    AnyRef → Any
  4. final def ==(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  5. final def ==(arg0: Any): Boolean

    Definition Classes
    Any
  6. final def asInstanceOf[T0]: T0

    Definition Classes
    Any
  7. def clone(): AnyRef

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  8. final def eq(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  9. def equals(arg0: Any): Boolean

    Definition Classes
    AnyRef → Any
  10. def finalize(): Unit

    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @throws( classOf[java.lang.Throwable] )
  11. final def getClass(): Class[_]

    Definition Classes
    AnyRef → Any
  12. def hashCode(): Int

    Definition Classes
    AnyRef → Any
  13. final def isInstanceOf[T0]: Boolean

    Definition Classes
    Any
  14. final def ne(arg0: AnyRef): Boolean

    Definition Classes
    AnyRef
  15. final def notify(): Unit

    Definition Classes
    AnyRef
  16. final def notifyAll(): Unit

    Definition Classes
    AnyRef
  17. final def synchronized[T0](arg0: ⇒ T0): T0

    Definition Classes
    AnyRef
  18. def toString(): String

    Definition Classes
    AnyRef → Any
  19. final def wait(): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  20. final def wait(arg0: Long, arg1: Int): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  21. final def wait(arg0: Long): Unit

    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped