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.
- Grouped
- Alphabetic
- By Inheritance
- intersection
- DocTrait
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- type &[L1 <: HList, L2 <: HList] = Merge[L1, L2]
-
trait
Compose
[R1 <: HList, P1 <: HList, R2 <: HList, P2 <: HList] extends Serializable
Type class witnessing that the requirements
R2in part can be fulfilled by the servicesP1provided bym1.Type class witnessing that the requirements
R2in part can be fulfilled by the servicesP1provided bym1.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
m2with some of the requirements met by m1. That is,m2': A & B & D => E - implicit class ComposeOps [R1 <: HList, P1 <: HList] extends AnyRef
-
sealed
trait
Join
[L1 <: HList, L2 <: HList] extends Serializable
Type class witnessing a common super (intersection) type of
L1andL2.Type class witnessing a common super (intersection) type of
L1andL2.An instance of this type class allows projection of
Outto bothL1andL2.The resulting intersection type
Outwill be similar to the result of a merge. However, withJoinit is not a problem if a member does occur in both of the components. In this caseJoinjust behaves like set-union ofL1andL2. - trait LowPrioCompose extends LowestPrioCompose
- trait LowPrioSubtype extends AnyRef
- trait LowestPrioCompose extends AnyRef
- trait Merge [L1 <: HList, L2 <: HList] extends Join[L1, L2]
- implicit class MergeOps [L1 <: HList] extends AnyRef
-
sealed
trait
Subtype
[L1 <: HList, L2 <: HList] extends Serializable
Type class witnessing the fact that the intersection type
L1is a subtype ofL2.Type class witnessing the fact that the intersection type
L1is a subtype ofL2.We also write:
L1 ≺ L2Note that also users can define instances of
SelectorandSubtypeto synthesize components of the intersection type from other existing components.Note: Right now, we don't require
L1orL2to be WF. - implicit class SubtypeOps [L1 <: HList] extends AnyRef
-
sealed
trait
WF
[L <: HList] extends Serializable
Type class witnessing the fact that L is a wellformed intersection type.
- type ^[L1 <: HList, L2 <: HList] = Join[L1, L2]
- type ≺[L1 <: HList, L2 <: HList] = Subtype[L1, L2]
Value Members
- implicit def autoLift[T](t: T): ::[T, HNil]
- def autoProject[T, L <: HList](l: L)(implicit s: ∈[T, L]): T
-
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.
- object Compose extends LowPrioCompose with Serializable
- object Join extends Serializable
- object Merge extends Serializable
- object Subtype extends LowPrioSubtype with Serializable
- object WF extends Serializable