Packages

  • package root
    Definition Classes
    root
  • package intersection

    Using HLists as flat representation of intersection types.

    Using HLists as flat representation of intersection types.

    Overview

    A value of the intersection type A & B can be used both as a value of type A as well as a value of type B. Given the two functions

    def f(x: Int): String = x.toString
    def g(x: Boolean): String = if (x) "hello" else "world"

    a value foo: Int & Boolean can be passed to both functions f and g. Using Scala's intersection types, which are backed by Java interfaces / classes we cannot obtain a value of type Int with Boolean. In addition, given an integer and a boolean, we cannot create such a value at runtime.

    This library allows to do both by representing a value which is of type Int and of type Boolean as the type-level list Int :: Boolean :: HNil. Merging an integer with a boolean value simply amounts to storing them in a (h)list.

    Usage

    Since we use the hlist implementation of shapeless as backing data structure you need to also include shapeless as dependency and import the hlist operations:

    import shapeless._
    import ops.hlist._

    Now, given two values of intersection types we can apply merge them to obtain the intersection:

    val x: Int :: HNil = 42 :: HNil
    val y: Boolean :: HNil = false :: HNil
    
    val both: Int :: Boolean :: HNil = x & y
    
    println(f(both.select[Int]) + " " + g(both.select[Boolean]))

    As you notice, the projection into the intersection type has to be explicitly selected at the moment. The same is true for subtyping relationships between intersection types:

    val y2: Boolean :: HNil = subsume[Boolean :: HNil, Int :: Boolean :: HNil](both)

    We hope to work around this issue in future releases.

    Implementation

    The implementation keeps the invariant, that a type may only occur once in an intersection (also see wellformedness, WF). Also the order of elements in the HLists can be arbitrary, effectively implementing type level sets.

    Design decision: Right now, also emtpy HLists are considered wellformed intersection types. The alternative would be to only allow singleton allow for empty intersections (see code which is commented out).

    We also don't account for subtypes, yet. So if B <: A and we have a list of type L = B :: HNil, we cannot select an A from it. This is a consequence of Selector being defined invariant in shapeless.

    Definition Classes
    root
  • Compose
  • ComposeOps
  • Join
  • LowPrioCompose
  • LowPrioSubtype
  • LowestPrioCompose
  • Merge
  • MergeOps
  • Subtype
  • SubtypeOps
  • WF
p

intersection

package intersection

Using HLists as flat representation of intersection types.

Overview

A value of the intersection type A & B can be used both as a value of type A as well as a value of type B. Given the two functions

def f(x: Int): String = x.toString
def g(x: Boolean): String = if (x) "hello" else "world"

a value foo: Int & Boolean can be passed to both functions f and g. Using Scala's intersection types, which are backed by Java interfaces / classes we cannot obtain a value of type Int with Boolean. In addition, given an integer and a boolean, we cannot create such a value at runtime.

This library allows to do both by representing a value which is of type Int and of type Boolean as the type-level list Int :: Boolean :: HNil. Merging an integer with a boolean value simply amounts to storing them in a (h)list.

Usage

Since we use the hlist implementation of shapeless as backing data structure you need to also include shapeless as dependency and import the hlist operations:

import shapeless._
import ops.hlist._

Now, given two values of intersection types we can apply merge them to obtain the intersection:

val x: Int :: HNil = 42 :: HNil
val y: Boolean :: HNil = false :: HNil

val both: Int :: Boolean :: HNil = x & y

println(f(both.select[Int]) + " " + g(both.select[Boolean]))

As you notice, the projection into the intersection type has to be explicitly selected at the moment. The same is true for subtyping relationships between intersection types:

val y2: Boolean :: HNil = subsume[Boolean :: HNil, Int :: Boolean :: HNil](both)

We hope to work around this issue in future releases.

Implementation

The implementation keeps the invariant, that a type may only occur once in an intersection (also see wellformedness, WF). Also the order of elements in the HLists can be arbitrary, effectively implementing type level sets.

Design decision: Right now, also emtpy HLists are considered wellformed intersection types. The alternative would be to only allow singleton allow for empty intersections (see code which is commented out).

We also don't account for subtypes, yet. So if B <: A and we have a list of type L = B :: HNil, we cannot select an A from it. This is a consequence of Selector being defined invariant in shapeless.

Linear Supertypes
DocTrait, AnyRef, Any
Ordering
  1. Grouped
  2. Alphabetic
  3. By Inheritance
Inherited
  1. intersection
  2. DocTrait
  3. AnyRef
  4. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Type Members

  1. type &[L1 <: HList, L2 <: HList] = Merge[L1, L2]

  2. trait Compose [R1 <: HList, P1 <: HList, R2 <: HList, P2 <: HList] extends Serializable

    Type class witnessing that the requirements R2 in part can be fulfilled by the services P1 provided by m1.

    Type class witnessing that the requirements R2 in part can be fulfilled by the services P1 provided by m1.

    The resulting module computes the same results as m2, but potentially has different requirements.

    Example

    Given two modules

    m1: A & B => C
    m2: C & D => E

    Compose should yield an updated module m2 with some of the requirements met by m1. That is,

    m2': A & B & D => E
  3. implicit class ComposeOps [R1 <: HList, P1 <: HList] extends AnyRef

  4. sealed trait Join [L1 <: HList, L2 <: HList] extends Serializable

    Type class witnessing a common super (intersection) type of L1 and L2.

    Type class witnessing a common super (intersection) type of L1 and L2.

    An instance of this type class allows projection of Out to both L1 and L2.

    The resulting intersection type Out will be similar to the result of a merge. However, with Join it is not a problem if a member does occur in both of the components. In this case Join just behaves like set-union of L1 and L2.

  5. trait LowPrioCompose extends LowestPrioCompose
  6. trait LowPrioSubtype extends AnyRef

  7. trait LowestPrioCompose extends AnyRef
  8. trait Merge [L1 <: HList, L2 <: HList] extends Join[L1, L2]

  9. implicit class MergeOps [L1 <: HList] extends AnyRef

  10. sealed trait Subtype [L1 <: HList, L2 <: HList] extends Serializable

    Type class witnessing the fact that the intersection type L1 is a subtype of L2.

    Type class witnessing the fact that the intersection type L1 is a subtype of L2.

    We also write: L1 ≺ L2

    Note that also users can define instances of Selector and Subtype to synthesize components of the intersection type from other existing components.

    Note: Right now, we don't require L1 or L2 to be WF.

  11. implicit class SubtypeOps [L1 <: HList] extends AnyRef

  12. sealed trait WF [L <: HList] extends Serializable

    Type class witnessing the fact that L is a wellformed intersection type.

  13. type ^[L1 <: HList, L2 <: HList] = Join[L1, L2]

  14. type ≺[L1 <: HList, L2 <: HList] = Subtype[L1, L2]

Value Members

  1. implicit def autoLift[T](t: T): ::[T, HNil]

  2. def autoProject[T, L <: HList](l: L)(implicit s: ∈[T, L]): T

  3. def subsume[L1 <: HList, L2 <: HList](l2: L2)(implicit s: ≺[L1, L2]): L1

    Note: Right now, applying subsumption implicitly does not work.

    Note: Right now, applying subsumption implicitly does not work. But making subsumption implicit also doesn't break anything right now, so let's keep it enabled.

  4. object Compose extends LowPrioCompose with Serializable

  5. object Join extends Serializable

  6. object Merge extends Serializable

  7. object Subtype extends LowPrioSubtype with Serializable

  8. object WF extends Serializable

Inherited from DocTrait

Inherited from AnyRef

Inherited from Any

Joining

Merging

Module Composition

Subtyping

Wellformedness

Ungrouped