Skip to content

New tests #3191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions tests/pos/eff-compose.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
object Test {

trait Effect

// Type X => Y
abstract class Fun[-X, Y] {
type Eff <: Effect
def apply(x: X): implicit Eff => Y
}

// Type X -> Y
type PureFun[-X, Y] = Fun[X, Y] { type Eff = Effect }

// def map(f: A => B)(xs: List[A]): List[B]
def map[A, B, E <: Effect](f: Fun[A, B] { type Eff = E})(xs: List[A])
: implicit E => List[B] =
xs.map(f.apply)

// def mapFn[A, B]: (A => B) -> List[A] -> List[B]
def mapFn[A, B, E <: Effect]:
PureFun[
Fun[A, B] { type Eff = E},
Fun[List[A], List[B]] { type Eff = E }
] =
new Fun[ // would like to write new PureFun here
// or, even better, drop everything
Fun[A, B] { type Eff = E},
Fun[List[A], List[B]] { type Eff = E }
] {
type Eff = Effect
def apply(f: Fun[A, B] { type Eff = E}) =
new Fun[List[A], List[B]] {
type Eff = E
def apply(xs: List[A]): implicit Eff => List[B] =
map(f)(xs)
}
}

implicit def combine[E1 <: Effect, E2 <: Effect](implicit x: E1, y: E2): E1 & E2 = ???

// def compose(f: A => B)(g: B => C)(x: A): C
def compose[A, B, C, E1 <: Effect, E2 <: Effect]
(f: Fun[A, B] { type Eff = E1})
(g: Fun[B, C] { type Eff = E2})
(x: A):
implicit E1 & E2 => C = g(f(x))

// def composeFn: (A => B) -> (B => C) -> A -> C
def composeFn[A, B, C, E1 <: Effect, E2 <: Effect]:
PureFun[
Fun[A, B] { type Eff = E1},
PureFun[
Fun[B, C] { type Eff = E2},
Fun[A, C] { type Eff = E1 & E2 }
]
] =
new Fun[
Fun[A, B] { type Eff = E1},
PureFun[
Fun[B, C] { type Eff = E2},
Fun[A, C] { type Eff = E1 & E2 }
]
] {
type Eff = Effect
def apply(f: Fun[A, B] { type Eff = E1}) =
new Fun[
Fun[B, C] { type Eff = E2},
Fun[A, C] { type Eff = E1 & E2 }
] {
type Eff = Effect
def apply(g: Fun[B, C] { type Eff = E2}) =
new Fun[A, C] {
type Eff = E1 & E2
def apply(x: A) = compose(f)(g)(x)
}
}
}
}
22 changes: 22 additions & 0 deletions tests/run/coop-equality.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
object Test extends App {

class ANY {
def ===(that: ANY) = this eq that
}

case class A(x: String) extends ANY {
def ===(that: A) = this.x == that.x
def ===(that: B) = this.x == that.x
}

case class B(x: String) extends ANY {
def ===(that: A) = this.x == that.x
def ===(that: B) = this.x == that.x
}

val a = A("")
val b = B("")

assert(a === b)
assert(!((a: ANY) === (b: ANY)))
}