Packages

trait Translator[I, O] extends (I) => Option[O]

Translator from an input I to an output O, designed to be built recursively.

Translation by Splitting and joining

The most import class of translators are constructed from

* a split map I => Option[X], where X is obtained from I by taking Tuples, Lists and sub-types

* a join map Y => Option[O], where Y is obtained from O structurally, in the same way as X is obtained from I, e.g. X = (I, I) and Y = (O, O).

However X and Y may involve taking sub-types independently of each other.

Such a translator splits an input, recursively translates X to Y and combines the result with the join (all steps work optionally).

Combinations, Basic Translators

Translators are built by combining others by OrElse, starting with basic translators specified by a function I => Option[O]. One can instead start with an empty translator.

Note that we can restrict the domain or extend the codomain of a translator by just using it as a function I => Option[O]. However this should be done after making all OrElse combinations, as the wrapped translator does not combine recursively.

A typical example of usage is as below

scala> import provingground._
import provingground._

scala> import translation._, Translator._
import translation._
import Translator._

scala> import cats._, cats.implicits._
import cats._
import cats.implicits._

scala> class A
defined class A

scala> case class B(x: A, y: A) extends A
defined class B

scala> case class C(x: A, y: A) extends A
defined class C

scala> case object D extends A
defined object D

scala> import Functors._
import Functors._

scala> val Bpat = Pattern.partial[A, II]{case B(x, y) => (x, y)}


scala> val Dpat = Pattern.partial[A, Un]{case D => ()}


scala> case object E extends A
defined object E

scala> val trans = Translator.Empty[A, A] || Bpat >>> {case (x, y) => C(x,y)} || Dpat >>> {case _ => E}
trans: provingground.translation.Translator.OrElse[A,A] = <function1>

scala> trans(B(B(D, D), B(B(D, D), D)))
res0: Option[A] = Some(C(C(E,E),C(C(E,E),E)))

scala> trans(C(D, D))
res1: Option[A] = None
Self Type
Translator[I, O]
Linear Supertypes
(I) => Option[O], AnyRef, Any
Type Hierarchy
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Translator
  2. Function1
  3. AnyRef
  4. Any
Implicitly
  1. by UnliftOps
  2. by any2stringadd
  3. by StringFormat
  4. by Ensuring
  5. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Abstract Value Members

  1. abstract def recTranslate(leafMap: => (I) => Option[O]): (I) => Option[O]

Concrete Value Members

  1. def andThen[A](g: (Option[O]) => A): (I) => A
    Definition Classes
    Function1
    Annotations
    @unspecialized()
  2. def apply(inp: I): Option[O]
    Definition Classes
    Translator → Function1
  3. def compose[A](g: (A) => I): (A) => Option[O]
    Definition Classes
    Function1
    Annotations
    @unspecialized()
  4. def elseOr(that: Translator[I, O]): OrElse[I, O]

    OrElse combinator, tries other translator first, then this one.

    OrElse combinator, tries other translator first, then this one. Other translator must be preprended

  5. def map[X](fn: (O) => X, ufn: (X) => O): Mapped[I, O, X]
  6. def orElse(that: Translator[I, O]): OrElse[I, O]

    OrElse combinator, tries other translator if the first fails.

  7. def toString(): String
    Definition Classes
    Function1 → AnyRef → Any
  8. def unlift: PartialFunction[I, O]
    Implicit
    This member is added by an implicit conversion from Translator[I, O] toUnliftOps[I, O] performed by method UnliftOps in scala.Function1.
    Definition Classes
    UnliftOps
  9. def ||(that: Translator[I, O]): OrElse[I, O]

    OrElse combinator, tries other translator if the first fails.

  10. def ||:(that: Translator[I, O]): OrElse[I, O]

    OrElse combinator, tries other translator first, then this one.

    OrElse combinator, tries other translator first, then this one. Other translator must be preprended