Class

edu.tum.cs.isabelle.Codec

Variant

Related Doc: package Codec

Permalink

abstract class Variant[A] extends AnyRef

Template for a codec for a sum type A.

Each constructor of A should get assigned a unique index when implementing this class. It is used to tag XML trees, so that the correct decoding function can be chosen.

In addition to the contract of Codec, instances of this class must also preserve the index, that is, any index returned by enc must produce a valid decoding function in dec.

Using this is a two-step process: First, create an object extending from this abstract and implement enc and dec accordingly. Second, call toCodec with an arbitrary tag on that object to obtain the actual codec. This process exists to work around a bug in the Scala compiler.

Example usage

new Variant[Option[A]] {
  def enc(env: Environment, a: Option[A]): (Int, env.XMLTree) = a match {
    case Some(a) => (0, Codec[A].encode(env)(a))
    case None    => (1, Codec[Unit].encode(env)(()))
  }
  def dec(env: Environment, idx: Int): Option[env.XMLTree => XMLResult[Option[A]]] = idx match {
    case 0 => Some(Codec[A].decode(env)(_).right.map(Some.apply))
    case 1 => Some(Codec[Unit].decode(env)(_).right.map(_ => None))
    case _ => None
  }
} toCodec "option"

Note the closing call to toCodec. The return type annotations are required for Scala 2.10.x.

Footnote

The preferred interface would look roughly like this:

def variant[A](env: Environment)(enc: A => (Int, env.XMLTree), dec: Int => Option[env.XMLTree => Result[A]], tag: String): Codec[A]

It can't be realised because some Scala versions have trouble establishing equivalence between path-dependent types.

Linear Supertypes
Ordering
  1. Alphabetic
  2. By inheritance
Inherited
  1. Variant
  2. AnyRef
  3. Any
  1. Hide All
  2. Show all
Visibility
  1. Public
  2. All

Instance Constructors

  1. new Variant()

    Permalink

Abstract Value Members

  1. abstract def dec(env: Environment, idx: Int): Option[(XMLTree) ⇒ XMLResult[A]]

    Permalink

    Given an index, produce the corresponding decoding function or nothing if the index is out of bounds.

  2. abstract def enc(env: Environment, a: A): (Int, XMLTree)

    Permalink

    Encode a value of the sum type into a tree and the index.

Concrete Value Members

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

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

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

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

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

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

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

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

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

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

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

    Permalink
    Definition Classes
    AnyRef
  13. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
  14. final def notifyAll(): Unit

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

    Permalink
    Definition Classes
    AnyRef
  16. def toCodec(tag: String): Codec[A]

    Permalink

    Turn an instance of this object into an actual Codec.

  17. def toString(): String

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

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

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

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Inherited from AnyRef

Inherited from Any

Ungrouped