Skip to content

Commit 57722f7

Browse files
committed
More tests
1 parent aa8b454 commit 57722f7

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

tests/pickling/i3479.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
sealed trait TermConstraint {
2+
final def neg: TermConstraint = this match {
3+
case Neg(c) => c
4+
case c: PositiveTermConstraint => Neg(c)
5+
}
6+
}
7+
case class Neg(c: PositiveTermConstraint) extends TermConstraint
8+
sealed trait PositiveTermConstraint extends TermConstraint
9+
10+
case object Dummy extends PositiveTermConstraint

tests/pickling/ops.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
object Test {
2+
3+
val num = implicitly[Integral[Int]]
4+
val ops = num.mkOrderingOps
5+
ops(9) < 10
6+
7+
}

tests/run/nats.scala

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
abstract class Nat {
2+
def toInt: Int
3+
}
4+
object Nat {
5+
case class S[N <: Nat](pred: N) extends Nat {
6+
def toInt = pred.toInt + 1
7+
}
8+
class Z extends Nat {
9+
def toInt = 0
10+
}
11+
val Z = new Z
12+
}
13+
14+
trait Plus[X <: Nat, Y <: Nat, R <: Nat] {
15+
def add(x: X, y: Y): R
16+
}
17+
18+
object Test {
19+
import Nat._
20+
implicit def zPlus[Y <: Nat]: Plus[Z, Y, Y] = new {
21+
def add(x: Z, y: Y): Y = y
22+
}
23+
implicit def sPlus[X <: Nat, Y <: Nat, R <: Nat](
24+
implicit ev: Plus[X, Y, R]
25+
): Plus[S[X], Y, S[R]] = new {
26+
def add(x: S[X], y: Y): S[R] = S(ev.add(x.pred, y))
27+
}
28+
def plus[X <: Nat, Y <: Nat, R <: Nat](x: X, y: Y)(
29+
implicit ev: Plus[X, Y, R]
30+
): R = ev.add(x, y)
31+
def main(args: Array[String]) = {
32+
val x = S(S(Z))
33+
val x1: S[S[Z]] = x
34+
val y = S(Z)
35+
val z = plus(x, y)
36+
val z1: S[S[S[Z]]] = z
37+
println(z.toInt)
38+
}
39+
}
40+

tests/run/nats.scala-deptypes

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
abstract class Nat {
2+
def toInt: Int
3+
}
4+
object Nat {
5+
case class S[N <: Nat](pred: N) extends Nat {
6+
def toInt = pred.toInt + 1
7+
}
8+
class Z extends Nat {
9+
def toInt = 0
10+
}
11+
val Z = new Z
12+
}
13+
14+
type NatOf(inline x: Int where x >= 0) =
15+
if (x == 0) Z
16+
else S[NatOf(x - 1)]
17+
18+
inline def natOf(inline x: Int where x >= 0): NatOf(x) =
19+
if (x == 0) Z
20+
else S(natOf(x - 1))
21+
22+
23+
24+
25+
26+
type Plus[X <: Nat, Y <: Nat] =
27+
if [X <:< Z] then Y
28+
else if [X <:< S[type X1]] then S[Plus[X1, Y]]
29+
30+
object Test {
31+
import Nat._
32+
def plus[X <: Nat, Y <: Nat](x: X, y: Y): Plus[X, Y] =
33+
if [X <:< Z] then y
34+
else if [X <:< S[_]] then S(plus(x.pred, y))
35+
36+
// Expanded version of `plus`:
37+
def plus2[X <: Nat, Y <: Nat](x: X, y: Y): Plus[X, Y] =
38+
if [X <:< Z] y
39+
else if [implicit ev: X <:< S[_]] then S(plus(ev(x).pred, y))
40+
41+
// This typechecks as follows:
42+
43+
// X <:< S[_]
44+
// X <:< S[T'] for some T' <: Nat
45+
// => ev(x): S[T']
46+
// => ev(x).pred: T'
47+
// => plus(ev(x).pred, y): Plus[T', Y]
48+
// => S(plus(ev(x).pred, y)): S[Plus[T', Y]]
49+
50+
// X <:< S[T']
51+
// => Plus[X, Y] = S[Plus[T', Y]]
52+
53+
// Expanded version of `plus`:
54+
def plus3[X <:< Nat, Y <:< Nat](x: Nat, y: Nat): Plus[x.type, y.type] = x match {
55+
case (x: Z) => y
56+
case S(x1) => S(plus(x1, y))
57+
}
58+
59+
60+
61+
62+
// x: Z
63+
// => x.type <:< Z
64+
// => Plus[x.type, y.type] = y.type
65+
66+
// x =?= S(x1)
67+
// => x.type <: S[T1], s1: T1
68+
// => plus(x1, y): Plus[T1, y.type]
69+
// =>
70+
71+
72+
def main(args: Array[String]) = {
73+
val x = S(S(Z))
74+
val x1: S[S[Z]] = x
75+
val y = S(Z)
76+
val z = plus(x, y)
77+
val z1: S[S[S[Z]]] = z
78+
println(z.toInt)
79+
}
80+
81+
type Concat[X <: Tuple, Y <: Tuple] =
82+
if [X <:< ()] then Y
83+
else if [X <:< (type F, type R)] then (F, Concat[R, Y])
84+
85+
def concat[X <: Tuple, Y <: Tuple](x: X, y: Y): Concat[X, Y] =
86+
if [X <:< ()] then Y
87+
else if [X <:< (type F, type R)] then (x.head, concat(x.tail, y))
88+
}
89+

0 commit comments

Comments
 (0)