From 21b147f7caf8b558b00044f493cf9da392c7e30e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 14 Feb 2003 14:39:24 +0000 Subject: Initial version. --- test/files/pos/A.scala | 8 +++ test/files/pos/IntSet.scala | 36 ++++++++++ test/files/pos/List1.scala | 45 +++++++++++++ test/files/pos/List2.scala | 139 +++++++++++++++++++++++++++++++++++++++ test/files/pos/Rational.scala | 25 +++++++ test/files/pos/X.scala | 14 ++++ test/files/pos/Y.scala | 7 ++ test/files/pos/Z.scala | 10 +++ test/files/pos/abstract.scala | 9 +++ test/files/pos/cls.scala | 17 +++++ test/files/pos/cls1.scala | 11 ++++ test/files/pos/clsrefine.scala | 33 ++++++++++ test/files/pos/cours1.scala | 54 +++++++++++++++ test/files/pos/cours2.scala | 128 +++++++++++++++++++++++++++++++++++ test/files/pos/cours2a.scala | 16 +++++ test/files/pos/cours2b.scala | 18 +++++ test/files/pos/cours2c.scala | 57 ++++++++++++++++ test/files/pos/eta.scala | 5 ++ test/files/pos/exceptions.scala | 6 ++ test/files/pos/imports.scala | 18 +++++ test/files/pos/lambda.scala | 8 +++ test/files/pos/lambdalift.scala | 15 +++++ test/files/pos/lambdalift1.scala | 17 +++++ test/files/pos/matthias1.scala | 15 +++++ test/files/pos/maxim1.scala | 5 ++ test/files/pos/michel1.scala | 9 +++ test/files/pos/michel2.scala | 7 ++ test/files/pos/michel3.scala | 3 + test/files/pos/michel4.scala | 7 ++ test/files/pos/michel5.scala | 5 ++ test/files/pos/modules.scala | 14 ++++ test/files/pos/modules1.scala | 14 ++++ test/files/pos/moduletrans.scala | 8 +++ test/files/pos/nested.scala | 27 ++++++++ test/files/pos/override.scala | 11 ++++ test/files/pos/patterns.scala | 13 ++++ test/files/pos/patterns2.scala | 16 +++++ test/files/pos/philippe1.scala | 8 +++ test/files/pos/philippe2.scala | 5 ++ test/files/pos/reftest.scala | 4 ++ test/files/pos/sort1.scala | 33 ++++++++++ test/files/pos/sqrt.scala | 21 ++++++ test/files/pos/stable.scala | 11 ++++ test/files/pos/strings.scala | 6 ++ test/files/pos/test1.scala | 5 ++ test/files/pos/test2.scala | 5 ++ test/files/pos/test4.scala | 45 +++++++++++++ test/files/pos/test4a.scala | 16 +++++ test/files/pos/test4refine.scala | 49 ++++++++++++++ test/files/pos/test5.scala | 68 +++++++++++++++++++ test/files/pos/test5refine.scala | 75 +++++++++++++++++++++ test/pos/A.scala | 8 +++ test/pos/IntSet.scala | 36 ++++++++++ test/pos/List1.scala | 45 +++++++++++++ test/pos/List2.scala | 139 +++++++++++++++++++++++++++++++++++++++ test/pos/Rational.scala | 25 +++++++ test/pos/X.scala | 14 ++++ test/pos/Y.scala | 7 ++ test/pos/Z.scala | 10 +++ test/pos/abstract.scala | 9 +++ test/pos/cls.scala | 17 +++++ test/pos/cls1.scala | 11 ++++ test/pos/clsrefine.scala | 33 ++++++++++ test/pos/cours1.scala | 54 +++++++++++++++ test/pos/cours2.scala | 128 +++++++++++++++++++++++++++++++++++ test/pos/cours2a.scala | 16 +++++ test/pos/cours2b.scala | 18 +++++ test/pos/cours2c.scala | 57 ++++++++++++++++ test/pos/eta.scala | 5 ++ test/pos/exceptions.scala | 6 ++ test/pos/imports.scala | 18 +++++ test/pos/lambda.scala | 8 +++ test/pos/lambdalift.scala | 15 +++++ test/pos/lambdalift1.scala | 17 +++++ test/pos/matthias1.scala | 15 +++++ test/pos/maxim1.scala | 5 ++ test/pos/michel1.scala | 9 +++ test/pos/michel2.scala | 7 ++ test/pos/michel3.scala | 3 + test/pos/michel4.scala | 7 ++ test/pos/michel5.scala | 5 ++ test/pos/modules.scala | 14 ++++ test/pos/modules1.scala | 14 ++++ test/pos/moduletrans.scala | 8 +++ test/pos/nested.scala | 27 ++++++++ test/pos/override.scala | 11 ++++ test/pos/patterns.scala | 13 ++++ test/pos/patterns2.scala | 16 +++++ test/pos/philippe1.scala | 8 +++ test/pos/philippe2.scala | 5 ++ test/pos/reftest.scala | 4 ++ test/pos/sort1.scala | 33 ++++++++++ test/pos/sqrt.scala | 21 ++++++ test/pos/stable.scala | 11 ++++ test/pos/strings.scala | 6 ++ test/pos/test1.scala | 5 ++ test/pos/test2.scala | 5 ++ test/pos/test4.scala | 45 +++++++++++++ test/pos/test4a.scala | 16 +++++ test/pos/test4refine.scala | 49 ++++++++++++++ test/pos/test5.scala | 68 +++++++++++++++++++ test/pos/test5refine.scala | 75 +++++++++++++++++++++ 102 files changed, 2402 insertions(+) create mode 100644 test/files/pos/A.scala create mode 100644 test/files/pos/IntSet.scala create mode 100644 test/files/pos/List1.scala create mode 100644 test/files/pos/List2.scala create mode 100644 test/files/pos/Rational.scala create mode 100644 test/files/pos/X.scala create mode 100644 test/files/pos/Y.scala create mode 100644 test/files/pos/Z.scala create mode 100644 test/files/pos/abstract.scala create mode 100644 test/files/pos/cls.scala create mode 100644 test/files/pos/cls1.scala create mode 100644 test/files/pos/clsrefine.scala create mode 100644 test/files/pos/cours1.scala create mode 100644 test/files/pos/cours2.scala create mode 100644 test/files/pos/cours2a.scala create mode 100644 test/files/pos/cours2b.scala create mode 100644 test/files/pos/cours2c.scala create mode 100644 test/files/pos/eta.scala create mode 100644 test/files/pos/exceptions.scala create mode 100644 test/files/pos/imports.scala create mode 100644 test/files/pos/lambda.scala create mode 100644 test/files/pos/lambdalift.scala create mode 100644 test/files/pos/lambdalift1.scala create mode 100644 test/files/pos/matthias1.scala create mode 100644 test/files/pos/maxim1.scala create mode 100644 test/files/pos/michel1.scala create mode 100644 test/files/pos/michel2.scala create mode 100644 test/files/pos/michel3.scala create mode 100644 test/files/pos/michel4.scala create mode 100644 test/files/pos/michel5.scala create mode 100644 test/files/pos/modules.scala create mode 100644 test/files/pos/modules1.scala create mode 100644 test/files/pos/moduletrans.scala create mode 100644 test/files/pos/nested.scala create mode 100644 test/files/pos/override.scala create mode 100644 test/files/pos/patterns.scala create mode 100644 test/files/pos/patterns2.scala create mode 100644 test/files/pos/philippe1.scala create mode 100644 test/files/pos/philippe2.scala create mode 100644 test/files/pos/reftest.scala create mode 100644 test/files/pos/sort1.scala create mode 100644 test/files/pos/sqrt.scala create mode 100644 test/files/pos/stable.scala create mode 100644 test/files/pos/strings.scala create mode 100644 test/files/pos/test1.scala create mode 100644 test/files/pos/test2.scala create mode 100644 test/files/pos/test4.scala create mode 100644 test/files/pos/test4a.scala create mode 100644 test/files/pos/test4refine.scala create mode 100644 test/files/pos/test5.scala create mode 100644 test/files/pos/test5refine.scala create mode 100644 test/pos/A.scala create mode 100644 test/pos/IntSet.scala create mode 100644 test/pos/List1.scala create mode 100644 test/pos/List2.scala create mode 100644 test/pos/Rational.scala create mode 100644 test/pos/X.scala create mode 100644 test/pos/Y.scala create mode 100644 test/pos/Z.scala create mode 100644 test/pos/abstract.scala create mode 100644 test/pos/cls.scala create mode 100644 test/pos/cls1.scala create mode 100644 test/pos/clsrefine.scala create mode 100644 test/pos/cours1.scala create mode 100644 test/pos/cours2.scala create mode 100644 test/pos/cours2a.scala create mode 100644 test/pos/cours2b.scala create mode 100644 test/pos/cours2c.scala create mode 100644 test/pos/eta.scala create mode 100644 test/pos/exceptions.scala create mode 100644 test/pos/imports.scala create mode 100644 test/pos/lambda.scala create mode 100644 test/pos/lambdalift.scala create mode 100644 test/pos/lambdalift1.scala create mode 100644 test/pos/matthias1.scala create mode 100644 test/pos/maxim1.scala create mode 100644 test/pos/michel1.scala create mode 100644 test/pos/michel2.scala create mode 100644 test/pos/michel3.scala create mode 100644 test/pos/michel4.scala create mode 100644 test/pos/michel5.scala create mode 100644 test/pos/modules.scala create mode 100644 test/pos/modules1.scala create mode 100644 test/pos/moduletrans.scala create mode 100644 test/pos/nested.scala create mode 100644 test/pos/override.scala create mode 100644 test/pos/patterns.scala create mode 100644 test/pos/patterns2.scala create mode 100644 test/pos/philippe1.scala create mode 100644 test/pos/philippe2.scala create mode 100644 test/pos/reftest.scala create mode 100644 test/pos/sort1.scala create mode 100644 test/pos/sqrt.scala create mode 100644 test/pos/stable.scala create mode 100644 test/pos/strings.scala create mode 100644 test/pos/test1.scala create mode 100644 test/pos/test2.scala create mode 100644 test/pos/test4.scala create mode 100644 test/pos/test4a.scala create mode 100644 test/pos/test4refine.scala create mode 100644 test/pos/test5.scala create mode 100644 test/pos/test5refine.scala (limited to 'test') diff --git a/test/files/pos/A.scala b/test/files/pos/A.scala new file mode 100644 index 0000000000..dd8cabaeb7 --- /dev/null +++ b/test/files/pos/A.scala @@ -0,0 +1,8 @@ +trait A extends scala.Object with {} + +module test { + + def x: A = x; + +} + diff --git a/test/files/pos/IntSet.scala b/test/files/pos/IntSet.scala new file mode 100644 index 0000000000..850a5cb923 --- /dev/null +++ b/test/files/pos/IntSet.scala @@ -0,0 +1,36 @@ +trait IntSet { + def incl(x: Int): IntSet; + def contains(x: Int): Boolean; + def foreach(f: Int => Unit): Unit; + def union(that: IntSet): IntSet; +} +module Empty extends IntSet { + def contains(x: Int): Boolean = Boolean.False; + def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty); + def foreach(f: Int => Unit): Unit = (); + def union(that: IntSet): IntSet = that; +} +class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { + def contains(x: Int): Boolean = + if (x < elem) left contains x + else if (x > elem) right contains x + else True; + def incl(x: Int): IntSet = + if (x < elem) new NonEmpty(elem, left incl x, right) + else if (x > elem) new NonEmpty(elem, left, right incl x) + else this; + def foreach(f: Int => Unit): Unit = { + left foreach f; + f(elem); + right foreach f; + } + def union(that: IntSet): IntSet = (left union (right union that)) incl elem; +} +module test { + def main = { + val x = Empty incl 1 incl 2; + val y = Empty incl 2 incl 3; + x foreach java.lang.System.out.println; + y foreach java.lang.System.out.println; + } +} \ No newline at end of file diff --git a/test/files/pos/List1.scala b/test/files/pos/List1.scala new file mode 100644 index 0000000000..60c23634bf --- /dev/null +++ b/test/files/pos/List1.scala @@ -0,0 +1,45 @@ +module lists { + + trait List[a] { + def isEmpty: Boolean; + def head: a; + def tail: List[a]; + def prepend(x: a) = Cons@[a](x, this); + } + + def Nil[a] = new List[a] { + def isEmpty = True; + def head = error@[a]("head of Nil"); + def tail = error@[List[a]]("tail of Nil"); + } + + def Cons[a](x: a, xs: List[a]): List[a] = new List[a] { + def isEmpty = Boolean.False; + def head = x; + def tail = xs; + } + + def foo = { + val intnil = Nil@[Int]; + val intlist = intnil.prepend(1).prepend(1+1); + val x: Int = intlist.head; + val strnil = Nil@[String]; + val strlist = strnil.prepend("A").prepend("AA"); + val y: String = strlist.head; + () + } + + class IntList() extends List[Int] { + def isEmpty: Boolean = False; + def head: Int = 1; + def foo: List[Int] with { def isEmpty: True.type; def head: Int; def tail: List[Int] } = Nil@[Int]; + def tail0: List[Int] = foo.prepend(1).prepend(1+1); + def tail: List[Int] = Nil@[Int].prepend(1).prepend(1+1); + } + + def foo2 = { + val il1 = new IntList(); + val il2 = il1.prepend(1).prepend(2); + () + } +} \ No newline at end of file diff --git a/test/files/pos/List2.scala b/test/files/pos/List2.scala new file mode 100644 index 0000000000..3d4087e538 --- /dev/null +++ b/test/files/pos/List2.scala @@ -0,0 +1,139 @@ +abstract final class List[a] with { + def ::(x: a): List[a] = cons(x, this); + + def head: a = match { + case [] => error("[].head"); + case x :: xs => x + } + + def tail: List[a] = match { + case [] => error("[].tail"); + case x :: xs => xs + } + + def isEmpty = match { + case [] => False; + case _ :: _ => True; + } + + def length: Int = match { + case [] => 0; + case x :: xs => 1 + xs.length; + } + + def ::: (that: List[a]): List[a] = match { + case [] => that; + case x :: xs => x :: xs ::: that + } + + def append(x: a): List[a] = this ::: x :: []; + + def map[b](f: (a)b): List[b] = match { + case [] => []; + case x :: xs => f(x) :: (xs map f) + } + + def flatMap[b](f: (a)List[b]): List[b] = match { + case [] => []; + case x :: xs => f(x) ::: (xs flatMap f) + } + + def filter(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) x :: (xs filter p) else xs filter p + } + + def foldl[b](f: (b, a)b)(z: b): b = match { + case [] => z; + case x :: xs => (xs foldl f)(f(z, head)) + } + + def foldr[b](f: (a, b)b)(z: b): b = match { + case [] => z; + case x :: xs => f(x, (xs foldr f)(z)) + } + + def foldl1(f: (a, a)a): a = match { + case [] => error("[].foldl1"); + case x :: xs => (xs foldl f)(x) + } + + def foldr1(f: (a, a)a): a = match { + case [] => error("[].foldr1"); + case x :: [] => x; + case x :: xs => f(x, (xs foldr1 f)) + } + + def forall(p: (a)Boolean): Boolean = match { + case [] => True; + case x :: xs => p(x) && (xs forall p) + } + + def exists(p: (a)Boolean): Boolean = match { + case [] => False; + case x :: xs => p(x) || (xs exists p) + } + + def take(n: Int): List[a] = match { + case [] => []; + case x :: xs => if (n == 0) [] else x :: (xs take (n - 1)) + } + + def drop(n: Int): List[a] = match { + case [] => []; + case x :: xs => if (n == 0) this else xs drop (n - 1) + } + + def takeWhile(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) x :: (xs takeWhile p) else [] + } + + def dropWhile(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) (xs dropWhile p) else this + } + + def init: List[a] = match { + case [] => error("[].init"); + case x :: [] => []; + case x :: xs => xs.init + } + + def last: a = match { + case [] => error("[].last"); + case x :: [] => x; + case x :: xs => xs.last + } + + def reverse: List[a] = { + def snoc(xs: List[a], x: a) = x :: xs; + foldl(snoc)([]) + } + + def zip[b](that: List[b]): List[(a,b)] = (this, that) match { + case (x :: xs, y :: ys) => (x, y) :: (xs zip ys) + case _ => [] + } + + override def toString(): String = "[" + mkString(",") + "]"; + def mkString(sep: String): String = match { + case [] => ""; + case x :: [] => x.toString(); + case x :: xs => x.toString() + sep + xs.toString() + } +} + +def error[a](x: String):a = (new java.lang.RuntimeException(x)).throw; + +case class Nil[b] extends List[b]; +case class ::_class[b](x: b)(xs: List[b]) extends List[b]; +def cons[a](x: a, xs: List[a]) = ::_class(x)(xs); +def nil[a] = new Nil[a]; + + + + + + + diff --git a/test/files/pos/Rational.scala b/test/files/pos/Rational.scala new file mode 100644 index 0000000000..14236975d4 --- /dev/null +++ b/test/files/pos/Rational.scala @@ -0,0 +1,25 @@ +class Rational(x: Int, y: Int) { + private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b); + private def g = gcd(x, y); + def numer = x / g; + def denom = y / g; + def add(r: Rational) = + new Rational( + numer * r.denom + r.numer * denom, + denom * r.denom); + def sub(r: Rational) = + new Rational( + numer * r.denom - r.numer * denom, + denom * r.denom); + def mul(r: Rational) = + new Rational( + numer * r.numer, + denom * r.denom); + def equal(r: Rational) = + new Rational( + numer * r.denom, + denom * r.numer); + + def asString: String = numer + "/" + denom; + override def toString(): String = numer + "/" + denom +} diff --git a/test/files/pos/X.scala b/test/files/pos/X.scala new file mode 100644 index 0000000000..2edf010efd --- /dev/null +++ b/test/files/pos/X.scala @@ -0,0 +1,14 @@ +abstract class A() { + + var x: Int + +} + +abstract class B() extends A() { + + var xx: Int = 0; + + def x = xx; + def x_=(y: Int) = xx = y; +} + diff --git a/test/files/pos/Y.scala b/test/files/pos/Y.scala new file mode 100644 index 0000000000..425d7fb346 --- /dev/null +++ b/test/files/pos/Y.scala @@ -0,0 +1,7 @@ +module test { + + def f(x: Object): Object = x.match; + +} + + diff --git a/test/files/pos/Z.scala b/test/files/pos/Z.scala new file mode 100644 index 0000000000..c19081e68c --- /dev/null +++ b/test/files/pos/Z.scala @@ -0,0 +1,10 @@ +trait X { + val elem: Int = 1 +} + +module test { + + def g(x: X) = x.elem; + def f(x: Object) = x.toString(); + +} diff --git a/test/files/pos/abstract.scala b/test/files/pos/abstract.scala new file mode 100644 index 0000000000..f51ae63e6b --- /dev/null +++ b/test/files/pos/abstract.scala @@ -0,0 +1,9 @@ +trait C { + type t; + def copy(x: t): t = x; +} + +class D() extends C { + type t = Int; + System.out.println(copy(1)); +} diff --git a/test/files/pos/cls.scala b/test/files/pos/cls.scala new file mode 100644 index 0000000000..3a46c306db --- /dev/null +++ b/test/files/pos/cls.scala @@ -0,0 +1,17 @@ +import scala._; + +package scalac.util { + +class A[X1, X2](x1: X1, x2: X2) {} +class B[Y](y1: Y, y2: Y) extends A[Y, Y](y1, y2) { + def f(x: Y, xs: B[Y]): Unit = {} + def g() = f(y1, this); +} + +module test { + val b: B[Int] = new B[Int](1, 2); + val a: A[Int, Int] = b; + val a1 = new A(1, "hello"); + val b1 = new B(1, "hello"); +} +} \ No newline at end of file diff --git a/test/files/pos/cls1.scala b/test/files/pos/cls1.scala new file mode 100644 index 0000000000..badc4e793a --- /dev/null +++ b/test/files/pos/cls1.scala @@ -0,0 +1,11 @@ +package cls1; + +trait A { + + type T; + + trait B extends A { + type T = A.this.T; + } + +} \ No newline at end of file diff --git a/test/files/pos/clsrefine.scala b/test/files/pos/clsrefine.scala new file mode 100644 index 0000000000..cbef0968e0 --- /dev/null +++ b/test/files/pos/clsrefine.scala @@ -0,0 +1,33 @@ +import scala._; + +package scalac.util { + +trait A { + type X1, X2; + val x1: X1, x2: X2; +} +trait B extends A { + type Y; + val y1: Y, y2: Y; + type X1 = Y, X2 = Y; + val x1 = y1, x2 = y2; + def f(x: Y, xs: B): Unit = {} + def g() = f(y1, this); +} + +module test { + val b: B with { type Y = Int } = new B { + type Y = Int; + val y1 = 1, y2 = 1; + } + val a: A with { type X1 = Int, X2 = Int } = b; + val a1 = new A with { + type X1 = Int, X2 = String; + val x1 = 1, x2 = "hello" + } + val b1 = new B with { + type Y = Any; + val y1 = 1, y2 = "hello"; + } +} +} \ No newline at end of file diff --git a/test/files/pos/cours1.scala b/test/files/pos/cours1.scala new file mode 100644 index 0000000000..48b5f6504d --- /dev/null +++ b/test/files/pos/cours1.scala @@ -0,0 +1,54 @@ +module test { + + def size = 2; + def pi = 3.14159; + def radius = 10; + def circumference = 2 * pi * radius; + def square(x: Double) = x * x; + def sumOfSquares(x: Double, y: Double) = square(x) + square(y); + def loop: Int = loop; + def first(x: Int, y: Int) = x; + def constOne(x: Int, def y: Int) = 1; + def abs(x: Double) = if (x >= 0) x else -x; + + def sqrtIter0(guess: Double, x: Double): Double = + if (isGoodEnough0(guess, x)) guess + else sqrtIter0(improve0(guess, x), x); + def improve0(guess: Double, x: Double) = + (guess + x / guess) / 2; + def isGoodEnough0(guess: Double, x: Double) = + abs(square(guess) - x) < 0.001; + def sqrt0(x: Double) = sqrtIter0(1.0, x); + + def sqrt1(x: Double) = { + def sqrtIter1(guess: Double, x: Double): Double = + if (isGoodEnough1(guess, x)) guess + else sqrtIter1(improve1(guess, x), x); + + def improve1(guess: Double, x: Double) = + (guess + x / guess) / 2; + + def isGoodEnough1(guess: Double, x: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter1(1.0, x); + } + + def sqrt2(x: Double) = { + def sqrtIter2(guess: Double): Double = + if (isGoodEnough2(guess)) guess + else sqrtIter2(improve2(guess)); + + def improve2(guess: Double) = + (guess + x / guess) / 2; + + def isGoodEnough2(guess: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter2(1.0); + } + + sqrt0(2); + sqrt1(2); + sqrt2(2); +} \ No newline at end of file diff --git a/test/files/pos/cours2.scala b/test/files/pos/cours2.scala new file mode 100644 index 0000000000..032531b25e --- /dev/null +++ b/test/files/pos/cours2.scala @@ -0,0 +1,128 @@ +module m1 { + + def id(x: Int): Double = x; + def cube(x: Int): Double = x * x * x; + def reciprocal(x: Int): Double = 1.0/x; + + def sumInts(a: Int, b: Int): Double = + if (a > b) 0 + else a + sumInts(a + 1, b); + + def sumCubes(a: Int, b: Int): Double = + if (a > b) 0 + else cube(a) + sumCubes(a + 1, b); + + def sumReciprocals(a: Int, b: Int): Double = + if (a > b) 0 + else 1.0/a + sumReciprocals(a, b); + +} + +module m2 { + + def id(x: Int): Double = x; + def cube(x: Int): Double = x * x * x; + def reciprocal(x: Int): Double = 1.0/x; + + def sum(f: Int => Double, a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f, a + 1, b); + + def sumInts(a: Int, b: Int): Double = sum(id, a, b); + def sumCubes(a: Int, b: Int): Double = sum(cube, a, b); + def sumReciprocals(a: Int, b: Int): Double = sum(reciprocal, a, b); +} + +module m3 { + + def sum(f: Int => Double, a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f, a + 1, b); + + def sumInts(a: Int, b: Int): Double = sum((x => x), a, b); + def sumCubes(a: Int, b: Int): Double = sum((x => x * x * x), a, b); + def sumReciprocals(a: Int, b: Int): Double = sum((x => 1.0/x), a, b); +} + +module m4 { + + def sum(f: Int => Double) = { + def sumF(a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sumF(a + 1, b); + sumF + } + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m5 { + + def sum(f: Int => Double): (Int, Int) => Double = (a, b) => + if (a > b) 0 + else f(a) + sum(f)(a + 1, b); + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m6 { + + def sum(f: Int => Double)(a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f)(a + 1, b); + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m7 { + + def sum(f: Int => Double)(a: Int, b: Int): Double = { + def iter(a: Int, result: Double): Double = + if (a > b) result + else iter(a + 1, f(a) + result); + iter(a, 0); + } + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m8 { + + def product(f: Int => Double)(a: Int, step: Int, b: Int): Double = + if (a > b) 1 + else f(a) * product(f)(a + step, step, b); + + val pi = 8 * product(x => x * x)(4, 2, 40) / product(x => x * x)(3, 2, 40); +} + +module m9 { + + def accumulate[t](combiner: (t, t) => t, nullValue: t, f: Int => t, next: Int => Int) + (a: Int, b: Int): t = + if (a > b) nullValue + else combiner(f(a), accumulate(combiner, nullValue, f, next)(next(a), b)); + + def inc(x: Int) = x + 1; + + def sum(f: Int => Double) = + accumulate((x: Double, y: Double) => x + y, 0, f, inc); + def product(f: Int => Double) = + accumulate((x: Double, y: Double) => x * y, 1, f, inc); +} + diff --git a/test/files/pos/cours2a.scala b/test/files/pos/cours2a.scala new file mode 100644 index 0000000000..b888b88a96 --- /dev/null +++ b/test/files/pos/cours2a.scala @@ -0,0 +1,16 @@ +module m1 { + def factorial(n: Int): Int = + if (n == 0) 1 + else n * factorial(n-1); +} + +module m2 { + + def factorial(n: Int): Int = { + def factIter(n: Int, acc: Int): Int = { + if (n == 0) acc + else factIter(n - 1, acc * n) + } + factIter(n, 1) + } +} diff --git a/test/files/pos/cours2b.scala b/test/files/pos/cours2b.scala new file mode 100644 index 0000000000..edac3ed483 --- /dev/null +++ b/test/files/pos/cours2b.scala @@ -0,0 +1,18 @@ +module m1 { + + def gcd(x: Int, y: Int): Int = + if (y == 0) x + else gcd(y, x % y); + + gcd(14, 21); + + def smallestDivisor(n: Int) = { + def findDivisor(d: Int): Int = + if (d * d > n) n + else if (n % d == 0) d + else findDivisor(d + 1); + findDivisor(2); + } + + def isPrime(n: Int) = smallestDivisor(n) == n; +} diff --git a/test/files/pos/cours2c.scala b/test/files/pos/cours2c.scala new file mode 100644 index 0000000000..c2e4b221d5 --- /dev/null +++ b/test/files/pos/cours2c.scala @@ -0,0 +1,57 @@ +module m1 { + + def average(x: Double, y: Double) = (x + y)/2; + def abs(x: Double): Double = if (x < 0) - x else x; + def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < 0.001; + + def search(f: Double => Double, negPoint: Double, posPoint: Double): Double = { + val midPoint = average(negPoint, posPoint); + if (isCloseEnough(negPoint, posPoint)) + midPoint + else { + val testVal = f(midPoint); + if (testVal > 0) search (f, negPoint, midPoint) + else if (testVal < 0) search (f, midPoint, posPoint) + else midPoint + } + } + + def halfIntervalMethod(f: Double => Double, a: Double, b: Double): Double = { + val aval = f(a); + val bval = f(b); + if (aval < 0 && bval > 0) search(f, a, b) + else if (bval < 0 && aval > 0) search(f, b, a) + else error("Values are not of opposite sign") + } +} + +module m2 { + + def abs(x: Double): Double = if (x < 0) - x else x; + def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < 0.001; + def average(x: Double, y: Double) = (x + y)/2; + + def fixedPoint(f: Double => Double, firstGuess: Double) = { + def try(guess: Double): Double = { + val next = f(guess); + if (isCloseEnough(guess, next)) next + else try(next) + } + try(firstGuess); + } + + def sin(x: Double): Double = x; + def cos(x: Double): Double = x; + + val result = fixedPoint((y => sin(y) + cos(y)), 1.0); + + def averageDamp(f: Double => Double)(x: Double) = + average(x, f(x)); + + def sqrt(x: Double) = + fixedPoint(averageDamp(y => x/y), 1.0); + + def cubeRoot(x: Double) = + fixedPoint(averageDamp(y => x/(y*y)), 1.0); +} + diff --git a/test/files/pos/eta.scala b/test/files/pos/eta.scala new file mode 100644 index 0000000000..67e6c0f080 --- /dev/null +++ b/test/files/pos/eta.scala @@ -0,0 +1,5 @@ +module test { + +def sum(f: Int => Int)(x: Int, y: Int): Int = 0; +def g = sum; +} \ No newline at end of file diff --git a/test/files/pos/exceptions.scala b/test/files/pos/exceptions.scala new file mode 100644 index 0000000000..93140ddbbe --- /dev/null +++ b/test/files/pos/exceptions.scala @@ -0,0 +1,6 @@ +module test { + + def error[a](x: String):a = new java.lang.RuntimeException(x) throw; + + def main = error("hi!"); +} \ No newline at end of file diff --git a/test/files/pos/imports.scala b/test/files/pos/imports.scala new file mode 100644 index 0000000000..f13a3a4368 --- /dev/null +++ b/test/files/pos/imports.scala @@ -0,0 +1,18 @@ +package test; + + + +import java.lang.{System => S} + +module test { + import S.out.{print => p, println => print} + + val foo = 1; + + p("hello"); print("world"); S.out.println("!"); + S.out.flush(); +} +module test1 { + import test._; + foo +} \ No newline at end of file diff --git a/test/files/pos/lambda.scala b/test/files/pos/lambda.scala new file mode 100644 index 0000000000..7e687df4ef --- /dev/null +++ b/test/files/pos/lambda.scala @@ -0,0 +1,8 @@ +module test { + + def apply[a,b](f: a => b): a => b = x: a => f(x); + + def twice[a](f: a => a): a => a = x: a => f(f(x)); + + def main = apply@[Int,Int](twice@[Int](x: Int => x))(1); +} \ No newline at end of file diff --git a/test/files/pos/lambdalift.scala b/test/files/pos/lambdalift.scala new file mode 100644 index 0000000000..f780b8e13f --- /dev/null +++ b/test/files/pos/lambdalift.scala @@ -0,0 +1,15 @@ +import scala._; + +module test { + + def f(x: Int) = { + def g() = h(); + def h() = x; + g(); + class inner() { + def g() = h(); + def h() = x; + } + g() + new inner().g(); + } +} \ No newline at end of file diff --git a/test/files/pos/lambdalift1.scala b/test/files/pos/lambdalift1.scala new file mode 100644 index 0000000000..e4c7b3a801 --- /dev/null +++ b/test/files/pos/lambdalift1.scala @@ -0,0 +1,17 @@ +import scala._; + +module test { + + def f[a <: java.lang.Object](x: a) = { + def print() = java.lang.System.out.println(x); + class A() { + def g() = { + class B() { + def h() = print() + } + new B().h() + } + } + new A().g() + } +} \ No newline at end of file diff --git a/test/files/pos/matthias1.scala b/test/files/pos/matthias1.scala new file mode 100644 index 0000000000..418c2f32be --- /dev/null +++ b/test/files/pos/matthias1.scala @@ -0,0 +1,15 @@ +class A() { + class B() { + def foo(x: B) = 0 + } +} +module test { + def main = { + val a = new A(); + val b = new a.B(); + val c = new a.B(); + val d = b.foo(c); + () + } +} + diff --git a/test/files/pos/maxim1.scala b/test/files/pos/maxim1.scala new file mode 100644 index 0000000000..01a7c05e9a --- /dev/null +++ b/test/files/pos/maxim1.scala @@ -0,0 +1,5 @@ +module test { + def f(x: Int)(y: Int) = x + y; + def y: Int => Int = f(2); + def main = y(1); +} diff --git a/test/files/pos/michel1.scala b/test/files/pos/michel1.scala new file mode 100644 index 0000000000..d341abbb87 --- /dev/null +++ b/test/files/pos/michel1.scala @@ -0,0 +1,9 @@ +class A[Ta] (a : Ta) { + def f = 1 +} + +trait C {} + +class B[Tb] (b : Tb) extends C with A[Tb] (b) with { + def g = 2 +} \ No newline at end of file diff --git a/test/files/pos/michel2.scala b/test/files/pos/michel2.scala new file mode 100644 index 0000000000..8f3f34e1ba --- /dev/null +++ b/test/files/pos/michel2.scala @@ -0,0 +1,7 @@ +trait A extends Object with { + def f : Int = 1 +} + +trait B extends Object with A with { + override def f : Int = super.f +} \ No newline at end of file diff --git a/test/files/pos/michel3.scala b/test/files/pos/michel3.scala new file mode 100644 index 0000000000..0e85295bfb --- /dev/null +++ b/test/files/pos/michel3.scala @@ -0,0 +1,3 @@ +abstract class A() { + val v : Int +} \ No newline at end of file diff --git a/test/files/pos/michel4.scala b/test/files/pos/michel4.scala new file mode 100644 index 0000000000..2eabe61605 --- /dev/null +++ b/test/files/pos/michel4.scala @@ -0,0 +1,7 @@ +class A() { + val f : Int = 2 +} + +class B() extends A() with { + override val f : Int = super.f +} \ No newline at end of file diff --git a/test/files/pos/michel5.scala b/test/files/pos/michel5.scala new file mode 100644 index 0000000000..2045737fc4 --- /dev/null +++ b/test/files/pos/michel5.scala @@ -0,0 +1,5 @@ +trait A[Ta] { } + +class B() extends Object with A[Int] with { + val x : Int = 2 +} \ No newline at end of file diff --git a/test/files/pos/modules.scala b/test/files/pos/modules.scala new file mode 100644 index 0000000000..d916d7fd60 --- /dev/null +++ b/test/files/pos/modules.scala @@ -0,0 +1,14 @@ +package scala { + + module a { + + module b { + + trait c {} + def foo(x: c): c = bar(x) + + } + + def bar(x: b.c): b.c = x + } +} diff --git a/test/files/pos/modules1.scala b/test/files/pos/modules1.scala new file mode 100644 index 0000000000..71c4bf8bd4 --- /dev/null +++ b/test/files/pos/modules1.scala @@ -0,0 +1,14 @@ +package scala { + + module a { + + module b { + + trait c {} + def foo(x: c): c = bar(x) + + } + + def bar(x: b.c): a.b.c = x + } +} diff --git a/test/files/pos/moduletrans.scala b/test/files/pos/moduletrans.scala new file mode 100644 index 0000000000..953ce4ee6c --- /dev/null +++ b/test/files/pos/moduletrans.scala @@ -0,0 +1,8 @@ +module m1 { + + class m() { + def f() = 5 + } + final val m: m = new m() + +} diff --git a/test/files/pos/nested.scala b/test/files/pos/nested.scala new file mode 100644 index 0000000000..e81b710d72 --- /dev/null +++ b/test/files/pos/nested.scala @@ -0,0 +1,27 @@ +// A non-trivial example of nested classes (mostly to test +// ExplicitOuterClasses). + +class A(pa : Int) { + def a1 = pa; + class B(pb : Int) { + def b1 = pa+pb+a1; + class C(pc : Int) extends A(b1) { + def c1 = pc+pb+pa + } + val c1 = new C(66) + } +} + +class M(x : Int) { + def m1 = x +} + +class A1(x : Int) extends A(x) with M(x) { + class D() extends B(42) { + val c2 = new C(66); + class E() extends C(5) { + def e1 = c1+b1+a1; + def e2 = new D(); + } + } +} diff --git a/test/files/pos/override.scala b/test/files/pos/override.scala new file mode 100644 index 0000000000..d930986913 --- /dev/null +++ b/test/files/pos/override.scala @@ -0,0 +1,11 @@ +trait A extends Object { + def f = 1 +} + +trait B extends Object { + def f = 2 +} + +trait C extends Object with A with B { + override def f = super.f +} diff --git a/test/files/pos/patterns.scala b/test/files/pos/patterns.scala new file mode 100644 index 0000000000..671ba096e3 --- /dev/null +++ b/test/files/pos/patterns.scala @@ -0,0 +1,13 @@ +trait Option[a] {} +case class Some[a](x: a) extends Option[a]; +case class None[a]() extends Option[a]; + +module test { + + def println(str: String): Unit = java.lang.System.out.println(str); + + def print(opt: Option[String]) = opt match { + case Some(x) => println(x); + case None() => println("nothing"); + } +} \ No newline at end of file diff --git a/test/files/pos/patterns2.scala b/test/files/pos/patterns2.scala new file mode 100644 index 0000000000..d10053bc01 --- /dev/null +++ b/test/files/pos/patterns2.scala @@ -0,0 +1,16 @@ +trait Option {} +case class Choice(a: Option, b: Option) extends Option; +case class Some(x: java.lang.String) extends Option; +case class None() extends Option; + +module test { + + def f(opt: Option) = opt match { + case Choice(Some("one"), Some(x)) => 1; + case Choice(Some("two"), None()) => 1; + case Choice(y, Some("two")) => 2; + case Choice(Some(z), a) => 3; + case Some(b) => 4; + case None() => 5; + } +} \ No newline at end of file diff --git a/test/files/pos/philippe1.scala b/test/files/pos/philippe1.scala new file mode 100644 index 0000000000..d079c5974a --- /dev/null +++ b/test/files/pos/philippe1.scala @@ -0,0 +1,8 @@ +module test { + def id[a](xs: Array[a]): Array[a] = xs; + + def main(args: Array[String]): Unit = { + val res: Array[String] = id(args); + () + } +} \ No newline at end of file diff --git a/test/files/pos/philippe2.scala b/test/files/pos/philippe2.scala new file mode 100644 index 0000000000..2f61fc46ca --- /dev/null +++ b/test/files/pos/philippe2.scala @@ -0,0 +1,5 @@ +class m1() { + def n() = 0; + def foo(i: Int)(j: Int): Unit = (); + val bar = foo(n()); +} diff --git a/test/files/pos/reftest.scala b/test/files/pos/reftest.scala new file mode 100644 index 0000000000..27083bd2cc --- /dev/null +++ b/test/files/pos/reftest.scala @@ -0,0 +1,4 @@ +import scala._; +module test { + val x: Ref[Int] = new Ref(1); +} \ No newline at end of file diff --git a/test/files/pos/sort1.scala b/test/files/pos/sort1.scala new file mode 100644 index 0000000000..0a75a87272 --- /dev/null +++ b/test/files/pos/sort1.scala @@ -0,0 +1,33 @@ +module test { + + type String = java.lang.String; + + def while(def c: Boolean)(def b: Unit): Unit = + if (c) { b ; while(c)(b) } + else (); + + def sort(a: Array[Double]): Unit = { + + def swap(i: Int, j: Int): Unit = { + val t = a(i) ; val u = a.apply(j) ; a(i) = u ; a(j) = t + } + + def sort1(l: Int, r: Int): Unit = { + val pivot = a((l + r) / 2); + var i = l, j = r; + while (i <= j) { + while (a(i) < pivot) { i = i + 1 } + while (a(j) > pivot) { j = j - 1 } + if (i <= j) { + swap(i, j); + i = i + 1; + j = j - 1; + } + } + if (l < j) sort1(l, j); + if (j < r) sort1(i, r); + } + + sort1(0, a.length - 1); + } +} \ No newline at end of file diff --git a/test/files/pos/sqrt.scala b/test/files/pos/sqrt.scala new file mode 100644 index 0000000000..f6a53824b4 --- /dev/null +++ b/test/files/pos/sqrt.scala @@ -0,0 +1,21 @@ +module test { + + def abs(x: Double) = if (x >= 0) x else 0 - x; + + + def square(x: Double) = x * x; + + def sqrt(x: Double) = { + def sqrtIter(guess: Double): Double = + if (isGoodEnough(guess)) guess + else sqrtIter(improve(guess)); + + def improve(guess: Double) = + (guess + x / guess) / 2; + + def isGoodEnough(guess: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter(1.0); + } +} \ No newline at end of file diff --git a/test/files/pos/stable.scala b/test/files/pos/stable.scala new file mode 100644 index 0000000000..3bbcad3799 --- /dev/null +++ b/test/files/pos/stable.scala @@ -0,0 +1,11 @@ +trait Base { + val x: Int; + val y: Int; + var z: Int; +} + +class Sub() extends Base with { + override val x: Int = 1; + override val y: Int = 2; + override var z: Int = 3; +} diff --git a/test/files/pos/strings.scala b/test/files/pos/strings.scala new file mode 100644 index 0000000000..75bf23f5e3 --- /dev/null +++ b/test/files/pos/strings.scala @@ -0,0 +1,6 @@ +// martin 1-3-2002: it seems there is a problem with the way Serializable is loaded. +module test { + + def f() = "hello".concat("world"); + +} diff --git a/test/files/pos/test1.scala b/test/files/pos/test1.scala new file mode 100644 index 0000000000..6e8e4f98a7 --- /dev/null +++ b/test/files/pos/test1.scala @@ -0,0 +1,5 @@ +module test { + + def f() = 5; + +} diff --git a/test/files/pos/test2.scala b/test/files/pos/test2.scala new file mode 100644 index 0000000000..4c73e0a10b --- /dev/null +++ b/test/files/pos/test2.scala @@ -0,0 +1,5 @@ +import scala._; +module test2 { + def f(x: Int): Int = 'a'; + def g(x: Int) = f(f(x)); +} \ No newline at end of file diff --git a/test/files/pos/test4.scala b/test/files/pos/test4.scala new file mode 100644 index 0000000000..c71b65a283 --- /dev/null +++ b/test/files/pos/test4.scala @@ -0,0 +1,45 @@ +trait C {} +trait D {} +trait E {} + +module test { + def c: C = c; + def d: D = d; + def e: E = e; +} + +import test._; + +trait S extends o.I[D] with { + def bar: E = foo(c,d); +} + +class O[X]() { + trait I[Y] { + def foo(x: X, y: Y): E = e; + } + val i:I[E] = null; + val j:I[X] = null; +} + +module o extends O[C]() { + + def main = { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar + } +} + +class Main() { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar; +} + diff --git a/test/files/pos/test4a.scala b/test/files/pos/test4a.scala new file mode 100644 index 0000000000..0d653c5012 --- /dev/null +++ b/test/files/pos/test4a.scala @@ -0,0 +1,16 @@ +trait C {} + +class O[X]() { + trait I[Y] { + def foo(y: Y): Y = y; + } + val j:I[X] = null; +} + +module o extends O[C]() { + def c: C = c; + def main = { + o.j.foo(c); + } +} + diff --git a/test/files/pos/test4refine.scala b/test/files/pos/test4refine.scala new file mode 100644 index 0000000000..8e3fee70cc --- /dev/null +++ b/test/files/pos/test4refine.scala @@ -0,0 +1,49 @@ +trait C {} +trait D {} +trait E {} + +module test { + def c: C = c; + def d: D = d; + def e: E = e; +} + +import test._; + +trait S extends o.I { + type Y = D; + def bar: E = foo(c,d); +} + +abstract class O() { + type X; + abstract trait I { + type Y; + def foo(x: X, y: Y): E = e; + } + val i:I with { type Y = E } = null; + val j:I with { type Y = X } = null; +} + +module o extends O() { + type X = C; + + def main = { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar + } +} + +class Main() { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar; +} + diff --git a/test/files/pos/test5.scala b/test/files/pos/test5.scala new file mode 100644 index 0000000000..4851e5e6a2 --- /dev/null +++ b/test/files/pos/test5.scala @@ -0,0 +1,68 @@ +import scala._; + +module test { + + trait F[If] {} + + def f[Jf](h: Jf):F[Jf] = f@[Jf](h); + + trait G[Ig] {} + + def g[Jg](h: Jg):G[Jg] = g@[Jg](h); + + class M[P]() { + class I[X]() { + // Methods to check the type X and P as seen from instances of I + def chk_ix(x: X): Unit = (); + def chk_ip(p: P): Unit {} + + // Value with type X as seen from instances of I + def val_ix: X = val_ix; + } + + val i:I[G[P]] = null; + + // Values with types P and i.X as seen from instances of M + def val_mp: P = val_mp; + def val_mix: G[P] = g@[P](val_mp); + } + + class N[Q]() extends M[F[Q]]() { + val j:J[G[Q]] = null; + + class J[Y]() extends I[G[Y]]() { + // Values with types Y and X as seen from instances of J + def val_jy: Y = val_jy; + def val_jx: G[Y] = g@[Y](val_jy); + + // Check type P + chk_ip(val_mp); + chk_ip(val_np); + } + + // Values with types Q, X.P, i.X, j.Y and j.X as seen from instances of N + def val_nq: Q = val_nq; + def val_np: F[Q] = f@[Q](val_nq); + def val_nix: G[F[Q]] = g@[F[Q]](val_np); + def val_njy: G[Q] = g@[Q](val_nq); + def val_njx: G[G[Q]] = g@[G[Q]](val_njy); + + // Check type i.P + i.chk_ip(val_mp); + i.chk_ip(val_np); + + // Check type j.P + j.chk_ip(val_mp); + j.chk_ip(val_np); + + // Check type i.X + i.chk_ix(i.val_ix); + i.chk_ix(val_mix); + i.chk_ix(val_nix); + + // Check j.X + j.chk_ix(j.val_ix); + j.chk_ix(j.val_jx); + j.chk_ix(val_njx); + } +} \ No newline at end of file diff --git a/test/files/pos/test5refine.scala b/test/files/pos/test5refine.scala new file mode 100644 index 0000000000..1097c6cff7 --- /dev/null +++ b/test/files/pos/test5refine.scala @@ -0,0 +1,75 @@ +import scala._; + +module test { + + abstract trait F { type If; } + + def f[Jf](h: Jf):F with { type If = Jf } = f@[Jf](h); + + abstract trait G { type Ig; } + + def g[Jg](h: Jg):G with { type Ig = Jg } = g@[Jg](h); + + abstract class M() { + type P; + abstract class I() { + type X; + + // Methods to check the type X and P as seen from instances of I + def chk_ix(x: X): Unit = {} + def chk_ip(p: P): Unit = {} + + // Value with type X as seen from instances of I + def val_ix: X = val_ix; + } + + val i: I with { type X = G with { type Ig = P } } = null; + + // Values with types P and i.X as seen from instances of M + def val_mp: P = val_mp; + def val_mix: G with { type Ig = P } = g@[P](val_mp); + } + + abstract class N() extends M() { + type Q; + type P = F with { type If = Q }; + val j:J with { type Y = G with { type Ig = Q } } = null; + + abstract class J() extends I() { + type Y; + type X = G with { type Ig = Y; }; + // Values with types Y and X as seen from instances of J + def val_jy: Y = val_jy; + def val_jx: G with { type Ig = Y; } = g@[Y](val_jy); + + // Check type P + chk_ip(val_mp); + chk_ip(val_np); + } + + // Values with types Q, X.P, i.X, j.Y and j.X as seen from instances of N + def val_nq: Q = val_nq; + def val_np: F with { type If = Q } = f@[Q](val_nq); + def val_nix: G with { type Ig = F with { type If = Q } } = g@[F with { type If = Q }](val_np); + def val_njy: G with { type Ig = Q; } = g@[Q](val_nq); + def val_njx: G with { type Ig = G with { type Ig = Q }} = g@[G with { type Ig = Q; }](val_njy); + + // Check type i.P + i.chk_ip(val_mp); + i.chk_ip(val_np); + + // Check type j.P + j.chk_ip(val_mp); + j.chk_ip(val_np); + + // Check type i.X + i.chk_ix(i.val_ix); + i.chk_ix(val_mix); + i.chk_ix(val_nix); + + // Check j.X + j.chk_ix(j.val_ix); + j.chk_ix(j.val_jx); + j.chk_ix(val_njx); + } +} \ No newline at end of file diff --git a/test/pos/A.scala b/test/pos/A.scala new file mode 100644 index 0000000000..dd8cabaeb7 --- /dev/null +++ b/test/pos/A.scala @@ -0,0 +1,8 @@ +trait A extends scala.Object with {} + +module test { + + def x: A = x; + +} + diff --git a/test/pos/IntSet.scala b/test/pos/IntSet.scala new file mode 100644 index 0000000000..850a5cb923 --- /dev/null +++ b/test/pos/IntSet.scala @@ -0,0 +1,36 @@ +trait IntSet { + def incl(x: Int): IntSet; + def contains(x: Int): Boolean; + def foreach(f: Int => Unit): Unit; + def union(that: IntSet): IntSet; +} +module Empty extends IntSet { + def contains(x: Int): Boolean = Boolean.False; + def incl(x: Int): IntSet = new NonEmpty(x, Empty, Empty); + def foreach(f: Int => Unit): Unit = (); + def union(that: IntSet): IntSet = that; +} +class NonEmpty(elem: Int, left: IntSet, right: IntSet) extends IntSet { + def contains(x: Int): Boolean = + if (x < elem) left contains x + else if (x > elem) right contains x + else True; + def incl(x: Int): IntSet = + if (x < elem) new NonEmpty(elem, left incl x, right) + else if (x > elem) new NonEmpty(elem, left, right incl x) + else this; + def foreach(f: Int => Unit): Unit = { + left foreach f; + f(elem); + right foreach f; + } + def union(that: IntSet): IntSet = (left union (right union that)) incl elem; +} +module test { + def main = { + val x = Empty incl 1 incl 2; + val y = Empty incl 2 incl 3; + x foreach java.lang.System.out.println; + y foreach java.lang.System.out.println; + } +} \ No newline at end of file diff --git a/test/pos/List1.scala b/test/pos/List1.scala new file mode 100644 index 0000000000..60c23634bf --- /dev/null +++ b/test/pos/List1.scala @@ -0,0 +1,45 @@ +module lists { + + trait List[a] { + def isEmpty: Boolean; + def head: a; + def tail: List[a]; + def prepend(x: a) = Cons@[a](x, this); + } + + def Nil[a] = new List[a] { + def isEmpty = True; + def head = error@[a]("head of Nil"); + def tail = error@[List[a]]("tail of Nil"); + } + + def Cons[a](x: a, xs: List[a]): List[a] = new List[a] { + def isEmpty = Boolean.False; + def head = x; + def tail = xs; + } + + def foo = { + val intnil = Nil@[Int]; + val intlist = intnil.prepend(1).prepend(1+1); + val x: Int = intlist.head; + val strnil = Nil@[String]; + val strlist = strnil.prepend("A").prepend("AA"); + val y: String = strlist.head; + () + } + + class IntList() extends List[Int] { + def isEmpty: Boolean = False; + def head: Int = 1; + def foo: List[Int] with { def isEmpty: True.type; def head: Int; def tail: List[Int] } = Nil@[Int]; + def tail0: List[Int] = foo.prepend(1).prepend(1+1); + def tail: List[Int] = Nil@[Int].prepend(1).prepend(1+1); + } + + def foo2 = { + val il1 = new IntList(); + val il2 = il1.prepend(1).prepend(2); + () + } +} \ No newline at end of file diff --git a/test/pos/List2.scala b/test/pos/List2.scala new file mode 100644 index 0000000000..3d4087e538 --- /dev/null +++ b/test/pos/List2.scala @@ -0,0 +1,139 @@ +abstract final class List[a] with { + def ::(x: a): List[a] = cons(x, this); + + def head: a = match { + case [] => error("[].head"); + case x :: xs => x + } + + def tail: List[a] = match { + case [] => error("[].tail"); + case x :: xs => xs + } + + def isEmpty = match { + case [] => False; + case _ :: _ => True; + } + + def length: Int = match { + case [] => 0; + case x :: xs => 1 + xs.length; + } + + def ::: (that: List[a]): List[a] = match { + case [] => that; + case x :: xs => x :: xs ::: that + } + + def append(x: a): List[a] = this ::: x :: []; + + def map[b](f: (a)b): List[b] = match { + case [] => []; + case x :: xs => f(x) :: (xs map f) + } + + def flatMap[b](f: (a)List[b]): List[b] = match { + case [] => []; + case x :: xs => f(x) ::: (xs flatMap f) + } + + def filter(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) x :: (xs filter p) else xs filter p + } + + def foldl[b](f: (b, a)b)(z: b): b = match { + case [] => z; + case x :: xs => (xs foldl f)(f(z, head)) + } + + def foldr[b](f: (a, b)b)(z: b): b = match { + case [] => z; + case x :: xs => f(x, (xs foldr f)(z)) + } + + def foldl1(f: (a, a)a): a = match { + case [] => error("[].foldl1"); + case x :: xs => (xs foldl f)(x) + } + + def foldr1(f: (a, a)a): a = match { + case [] => error("[].foldr1"); + case x :: [] => x; + case x :: xs => f(x, (xs foldr1 f)) + } + + def forall(p: (a)Boolean): Boolean = match { + case [] => True; + case x :: xs => p(x) && (xs forall p) + } + + def exists(p: (a)Boolean): Boolean = match { + case [] => False; + case x :: xs => p(x) || (xs exists p) + } + + def take(n: Int): List[a] = match { + case [] => []; + case x :: xs => if (n == 0) [] else x :: (xs take (n - 1)) + } + + def drop(n: Int): List[a] = match { + case [] => []; + case x :: xs => if (n == 0) this else xs drop (n - 1) + } + + def takeWhile(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) x :: (xs takeWhile p) else [] + } + + def dropWhile(p: (a)Boolean): List[a] = match { + case [] => []; + case x :: xs => if (p(x)) (xs dropWhile p) else this + } + + def init: List[a] = match { + case [] => error("[].init"); + case x :: [] => []; + case x :: xs => xs.init + } + + def last: a = match { + case [] => error("[].last"); + case x :: [] => x; + case x :: xs => xs.last + } + + def reverse: List[a] = { + def snoc(xs: List[a], x: a) = x :: xs; + foldl(snoc)([]) + } + + def zip[b](that: List[b]): List[(a,b)] = (this, that) match { + case (x :: xs, y :: ys) => (x, y) :: (xs zip ys) + case _ => [] + } + + override def toString(): String = "[" + mkString(",") + "]"; + def mkString(sep: String): String = match { + case [] => ""; + case x :: [] => x.toString(); + case x :: xs => x.toString() + sep + xs.toString() + } +} + +def error[a](x: String):a = (new java.lang.RuntimeException(x)).throw; + +case class Nil[b] extends List[b]; +case class ::_class[b](x: b)(xs: List[b]) extends List[b]; +def cons[a](x: a, xs: List[a]) = ::_class(x)(xs); +def nil[a] = new Nil[a]; + + + + + + + diff --git a/test/pos/Rational.scala b/test/pos/Rational.scala new file mode 100644 index 0000000000..14236975d4 --- /dev/null +++ b/test/pos/Rational.scala @@ -0,0 +1,25 @@ +class Rational(x: Int, y: Int) { + private def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b); + private def g = gcd(x, y); + def numer = x / g; + def denom = y / g; + def add(r: Rational) = + new Rational( + numer * r.denom + r.numer * denom, + denom * r.denom); + def sub(r: Rational) = + new Rational( + numer * r.denom - r.numer * denom, + denom * r.denom); + def mul(r: Rational) = + new Rational( + numer * r.numer, + denom * r.denom); + def equal(r: Rational) = + new Rational( + numer * r.denom, + denom * r.numer); + + def asString: String = numer + "/" + denom; + override def toString(): String = numer + "/" + denom +} diff --git a/test/pos/X.scala b/test/pos/X.scala new file mode 100644 index 0000000000..2edf010efd --- /dev/null +++ b/test/pos/X.scala @@ -0,0 +1,14 @@ +abstract class A() { + + var x: Int + +} + +abstract class B() extends A() { + + var xx: Int = 0; + + def x = xx; + def x_=(y: Int) = xx = y; +} + diff --git a/test/pos/Y.scala b/test/pos/Y.scala new file mode 100644 index 0000000000..425d7fb346 --- /dev/null +++ b/test/pos/Y.scala @@ -0,0 +1,7 @@ +module test { + + def f(x: Object): Object = x.match; + +} + + diff --git a/test/pos/Z.scala b/test/pos/Z.scala new file mode 100644 index 0000000000..c19081e68c --- /dev/null +++ b/test/pos/Z.scala @@ -0,0 +1,10 @@ +trait X { + val elem: Int = 1 +} + +module test { + + def g(x: X) = x.elem; + def f(x: Object) = x.toString(); + +} diff --git a/test/pos/abstract.scala b/test/pos/abstract.scala new file mode 100644 index 0000000000..f51ae63e6b --- /dev/null +++ b/test/pos/abstract.scala @@ -0,0 +1,9 @@ +trait C { + type t; + def copy(x: t): t = x; +} + +class D() extends C { + type t = Int; + System.out.println(copy(1)); +} diff --git a/test/pos/cls.scala b/test/pos/cls.scala new file mode 100644 index 0000000000..3a46c306db --- /dev/null +++ b/test/pos/cls.scala @@ -0,0 +1,17 @@ +import scala._; + +package scalac.util { + +class A[X1, X2](x1: X1, x2: X2) {} +class B[Y](y1: Y, y2: Y) extends A[Y, Y](y1, y2) { + def f(x: Y, xs: B[Y]): Unit = {} + def g() = f(y1, this); +} + +module test { + val b: B[Int] = new B[Int](1, 2); + val a: A[Int, Int] = b; + val a1 = new A(1, "hello"); + val b1 = new B(1, "hello"); +} +} \ No newline at end of file diff --git a/test/pos/cls1.scala b/test/pos/cls1.scala new file mode 100644 index 0000000000..badc4e793a --- /dev/null +++ b/test/pos/cls1.scala @@ -0,0 +1,11 @@ +package cls1; + +trait A { + + type T; + + trait B extends A { + type T = A.this.T; + } + +} \ No newline at end of file diff --git a/test/pos/clsrefine.scala b/test/pos/clsrefine.scala new file mode 100644 index 0000000000..cbef0968e0 --- /dev/null +++ b/test/pos/clsrefine.scala @@ -0,0 +1,33 @@ +import scala._; + +package scalac.util { + +trait A { + type X1, X2; + val x1: X1, x2: X2; +} +trait B extends A { + type Y; + val y1: Y, y2: Y; + type X1 = Y, X2 = Y; + val x1 = y1, x2 = y2; + def f(x: Y, xs: B): Unit = {} + def g() = f(y1, this); +} + +module test { + val b: B with { type Y = Int } = new B { + type Y = Int; + val y1 = 1, y2 = 1; + } + val a: A with { type X1 = Int, X2 = Int } = b; + val a1 = new A with { + type X1 = Int, X2 = String; + val x1 = 1, x2 = "hello" + } + val b1 = new B with { + type Y = Any; + val y1 = 1, y2 = "hello"; + } +} +} \ No newline at end of file diff --git a/test/pos/cours1.scala b/test/pos/cours1.scala new file mode 100644 index 0000000000..48b5f6504d --- /dev/null +++ b/test/pos/cours1.scala @@ -0,0 +1,54 @@ +module test { + + def size = 2; + def pi = 3.14159; + def radius = 10; + def circumference = 2 * pi * radius; + def square(x: Double) = x * x; + def sumOfSquares(x: Double, y: Double) = square(x) + square(y); + def loop: Int = loop; + def first(x: Int, y: Int) = x; + def constOne(x: Int, def y: Int) = 1; + def abs(x: Double) = if (x >= 0) x else -x; + + def sqrtIter0(guess: Double, x: Double): Double = + if (isGoodEnough0(guess, x)) guess + else sqrtIter0(improve0(guess, x), x); + def improve0(guess: Double, x: Double) = + (guess + x / guess) / 2; + def isGoodEnough0(guess: Double, x: Double) = + abs(square(guess) - x) < 0.001; + def sqrt0(x: Double) = sqrtIter0(1.0, x); + + def sqrt1(x: Double) = { + def sqrtIter1(guess: Double, x: Double): Double = + if (isGoodEnough1(guess, x)) guess + else sqrtIter1(improve1(guess, x), x); + + def improve1(guess: Double, x: Double) = + (guess + x / guess) / 2; + + def isGoodEnough1(guess: Double, x: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter1(1.0, x); + } + + def sqrt2(x: Double) = { + def sqrtIter2(guess: Double): Double = + if (isGoodEnough2(guess)) guess + else sqrtIter2(improve2(guess)); + + def improve2(guess: Double) = + (guess + x / guess) / 2; + + def isGoodEnough2(guess: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter2(1.0); + } + + sqrt0(2); + sqrt1(2); + sqrt2(2); +} \ No newline at end of file diff --git a/test/pos/cours2.scala b/test/pos/cours2.scala new file mode 100644 index 0000000000..032531b25e --- /dev/null +++ b/test/pos/cours2.scala @@ -0,0 +1,128 @@ +module m1 { + + def id(x: Int): Double = x; + def cube(x: Int): Double = x * x * x; + def reciprocal(x: Int): Double = 1.0/x; + + def sumInts(a: Int, b: Int): Double = + if (a > b) 0 + else a + sumInts(a + 1, b); + + def sumCubes(a: Int, b: Int): Double = + if (a > b) 0 + else cube(a) + sumCubes(a + 1, b); + + def sumReciprocals(a: Int, b: Int): Double = + if (a > b) 0 + else 1.0/a + sumReciprocals(a, b); + +} + +module m2 { + + def id(x: Int): Double = x; + def cube(x: Int): Double = x * x * x; + def reciprocal(x: Int): Double = 1.0/x; + + def sum(f: Int => Double, a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f, a + 1, b); + + def sumInts(a: Int, b: Int): Double = sum(id, a, b); + def sumCubes(a: Int, b: Int): Double = sum(cube, a, b); + def sumReciprocals(a: Int, b: Int): Double = sum(reciprocal, a, b); +} + +module m3 { + + def sum(f: Int => Double, a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f, a + 1, b); + + def sumInts(a: Int, b: Int): Double = sum((x => x), a, b); + def sumCubes(a: Int, b: Int): Double = sum((x => x * x * x), a, b); + def sumReciprocals(a: Int, b: Int): Double = sum((x => 1.0/x), a, b); +} + +module m4 { + + def sum(f: Int => Double) = { + def sumF(a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sumF(a + 1, b); + sumF + } + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m5 { + + def sum(f: Int => Double): (Int, Int) => Double = (a, b) => + if (a > b) 0 + else f(a) + sum(f)(a + 1, b); + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m6 { + + def sum(f: Int => Double)(a: Int, b: Int): Double = + if (a > b) 0 + else f(a) + sum(f)(a + 1, b); + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m7 { + + def sum(f: Int => Double)(a: Int, b: Int): Double = { + def iter(a: Int, result: Double): Double = + if (a > b) result + else iter(a + 1, f(a) + result); + iter(a, 0); + } + + def sumInts = sum(x => x); + def sumCubes = sum(x => x * x * x); + def sumReciprocals = sum(x => 1.0/x); + + sumCubes(1, 10) + sumReciprocals(10, 20); +} + +module m8 { + + def product(f: Int => Double)(a: Int, step: Int, b: Int): Double = + if (a > b) 1 + else f(a) * product(f)(a + step, step, b); + + val pi = 8 * product(x => x * x)(4, 2, 40) / product(x => x * x)(3, 2, 40); +} + +module m9 { + + def accumulate[t](combiner: (t, t) => t, nullValue: t, f: Int => t, next: Int => Int) + (a: Int, b: Int): t = + if (a > b) nullValue + else combiner(f(a), accumulate(combiner, nullValue, f, next)(next(a), b)); + + def inc(x: Int) = x + 1; + + def sum(f: Int => Double) = + accumulate((x: Double, y: Double) => x + y, 0, f, inc); + def product(f: Int => Double) = + accumulate((x: Double, y: Double) => x * y, 1, f, inc); +} + diff --git a/test/pos/cours2a.scala b/test/pos/cours2a.scala new file mode 100644 index 0000000000..b888b88a96 --- /dev/null +++ b/test/pos/cours2a.scala @@ -0,0 +1,16 @@ +module m1 { + def factorial(n: Int): Int = + if (n == 0) 1 + else n * factorial(n-1); +} + +module m2 { + + def factorial(n: Int): Int = { + def factIter(n: Int, acc: Int): Int = { + if (n == 0) acc + else factIter(n - 1, acc * n) + } + factIter(n, 1) + } +} diff --git a/test/pos/cours2b.scala b/test/pos/cours2b.scala new file mode 100644 index 0000000000..edac3ed483 --- /dev/null +++ b/test/pos/cours2b.scala @@ -0,0 +1,18 @@ +module m1 { + + def gcd(x: Int, y: Int): Int = + if (y == 0) x + else gcd(y, x % y); + + gcd(14, 21); + + def smallestDivisor(n: Int) = { + def findDivisor(d: Int): Int = + if (d * d > n) n + else if (n % d == 0) d + else findDivisor(d + 1); + findDivisor(2); + } + + def isPrime(n: Int) = smallestDivisor(n) == n; +} diff --git a/test/pos/cours2c.scala b/test/pos/cours2c.scala new file mode 100644 index 0000000000..c2e4b221d5 --- /dev/null +++ b/test/pos/cours2c.scala @@ -0,0 +1,57 @@ +module m1 { + + def average(x: Double, y: Double) = (x + y)/2; + def abs(x: Double): Double = if (x < 0) - x else x; + def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < 0.001; + + def search(f: Double => Double, negPoint: Double, posPoint: Double): Double = { + val midPoint = average(negPoint, posPoint); + if (isCloseEnough(negPoint, posPoint)) + midPoint + else { + val testVal = f(midPoint); + if (testVal > 0) search (f, negPoint, midPoint) + else if (testVal < 0) search (f, midPoint, posPoint) + else midPoint + } + } + + def halfIntervalMethod(f: Double => Double, a: Double, b: Double): Double = { + val aval = f(a); + val bval = f(b); + if (aval < 0 && bval > 0) search(f, a, b) + else if (bval < 0 && aval > 0) search(f, b, a) + else error("Values are not of opposite sign") + } +} + +module m2 { + + def abs(x: Double): Double = if (x < 0) - x else x; + def isCloseEnough(x: Double, y: Double) = abs((x - y) / x) < 0.001; + def average(x: Double, y: Double) = (x + y)/2; + + def fixedPoint(f: Double => Double, firstGuess: Double) = { + def try(guess: Double): Double = { + val next = f(guess); + if (isCloseEnough(guess, next)) next + else try(next) + } + try(firstGuess); + } + + def sin(x: Double): Double = x; + def cos(x: Double): Double = x; + + val result = fixedPoint((y => sin(y) + cos(y)), 1.0); + + def averageDamp(f: Double => Double)(x: Double) = + average(x, f(x)); + + def sqrt(x: Double) = + fixedPoint(averageDamp(y => x/y), 1.0); + + def cubeRoot(x: Double) = + fixedPoint(averageDamp(y => x/(y*y)), 1.0); +} + diff --git a/test/pos/eta.scala b/test/pos/eta.scala new file mode 100644 index 0000000000..67e6c0f080 --- /dev/null +++ b/test/pos/eta.scala @@ -0,0 +1,5 @@ +module test { + +def sum(f: Int => Int)(x: Int, y: Int): Int = 0; +def g = sum; +} \ No newline at end of file diff --git a/test/pos/exceptions.scala b/test/pos/exceptions.scala new file mode 100644 index 0000000000..93140ddbbe --- /dev/null +++ b/test/pos/exceptions.scala @@ -0,0 +1,6 @@ +module test { + + def error[a](x: String):a = new java.lang.RuntimeException(x) throw; + + def main = error("hi!"); +} \ No newline at end of file diff --git a/test/pos/imports.scala b/test/pos/imports.scala new file mode 100644 index 0000000000..f13a3a4368 --- /dev/null +++ b/test/pos/imports.scala @@ -0,0 +1,18 @@ +package test; + + + +import java.lang.{System => S} + +module test { + import S.out.{print => p, println => print} + + val foo = 1; + + p("hello"); print("world"); S.out.println("!"); + S.out.flush(); +} +module test1 { + import test._; + foo +} \ No newline at end of file diff --git a/test/pos/lambda.scala b/test/pos/lambda.scala new file mode 100644 index 0000000000..7e687df4ef --- /dev/null +++ b/test/pos/lambda.scala @@ -0,0 +1,8 @@ +module test { + + def apply[a,b](f: a => b): a => b = x: a => f(x); + + def twice[a](f: a => a): a => a = x: a => f(f(x)); + + def main = apply@[Int,Int](twice@[Int](x: Int => x))(1); +} \ No newline at end of file diff --git a/test/pos/lambdalift.scala b/test/pos/lambdalift.scala new file mode 100644 index 0000000000..f780b8e13f --- /dev/null +++ b/test/pos/lambdalift.scala @@ -0,0 +1,15 @@ +import scala._; + +module test { + + def f(x: Int) = { + def g() = h(); + def h() = x; + g(); + class inner() { + def g() = h(); + def h() = x; + } + g() + new inner().g(); + } +} \ No newline at end of file diff --git a/test/pos/lambdalift1.scala b/test/pos/lambdalift1.scala new file mode 100644 index 0000000000..e4c7b3a801 --- /dev/null +++ b/test/pos/lambdalift1.scala @@ -0,0 +1,17 @@ +import scala._; + +module test { + + def f[a <: java.lang.Object](x: a) = { + def print() = java.lang.System.out.println(x); + class A() { + def g() = { + class B() { + def h() = print() + } + new B().h() + } + } + new A().g() + } +} \ No newline at end of file diff --git a/test/pos/matthias1.scala b/test/pos/matthias1.scala new file mode 100644 index 0000000000..418c2f32be --- /dev/null +++ b/test/pos/matthias1.scala @@ -0,0 +1,15 @@ +class A() { + class B() { + def foo(x: B) = 0 + } +} +module test { + def main = { + val a = new A(); + val b = new a.B(); + val c = new a.B(); + val d = b.foo(c); + () + } +} + diff --git a/test/pos/maxim1.scala b/test/pos/maxim1.scala new file mode 100644 index 0000000000..01a7c05e9a --- /dev/null +++ b/test/pos/maxim1.scala @@ -0,0 +1,5 @@ +module test { + def f(x: Int)(y: Int) = x + y; + def y: Int => Int = f(2); + def main = y(1); +} diff --git a/test/pos/michel1.scala b/test/pos/michel1.scala new file mode 100644 index 0000000000..d341abbb87 --- /dev/null +++ b/test/pos/michel1.scala @@ -0,0 +1,9 @@ +class A[Ta] (a : Ta) { + def f = 1 +} + +trait C {} + +class B[Tb] (b : Tb) extends C with A[Tb] (b) with { + def g = 2 +} \ No newline at end of file diff --git a/test/pos/michel2.scala b/test/pos/michel2.scala new file mode 100644 index 0000000000..8f3f34e1ba --- /dev/null +++ b/test/pos/michel2.scala @@ -0,0 +1,7 @@ +trait A extends Object with { + def f : Int = 1 +} + +trait B extends Object with A with { + override def f : Int = super.f +} \ No newline at end of file diff --git a/test/pos/michel3.scala b/test/pos/michel3.scala new file mode 100644 index 0000000000..0e85295bfb --- /dev/null +++ b/test/pos/michel3.scala @@ -0,0 +1,3 @@ +abstract class A() { + val v : Int +} \ No newline at end of file diff --git a/test/pos/michel4.scala b/test/pos/michel4.scala new file mode 100644 index 0000000000..2eabe61605 --- /dev/null +++ b/test/pos/michel4.scala @@ -0,0 +1,7 @@ +class A() { + val f : Int = 2 +} + +class B() extends A() with { + override val f : Int = super.f +} \ No newline at end of file diff --git a/test/pos/michel5.scala b/test/pos/michel5.scala new file mode 100644 index 0000000000..2045737fc4 --- /dev/null +++ b/test/pos/michel5.scala @@ -0,0 +1,5 @@ +trait A[Ta] { } + +class B() extends Object with A[Int] with { + val x : Int = 2 +} \ No newline at end of file diff --git a/test/pos/modules.scala b/test/pos/modules.scala new file mode 100644 index 0000000000..d916d7fd60 --- /dev/null +++ b/test/pos/modules.scala @@ -0,0 +1,14 @@ +package scala { + + module a { + + module b { + + trait c {} + def foo(x: c): c = bar(x) + + } + + def bar(x: b.c): b.c = x + } +} diff --git a/test/pos/modules1.scala b/test/pos/modules1.scala new file mode 100644 index 0000000000..71c4bf8bd4 --- /dev/null +++ b/test/pos/modules1.scala @@ -0,0 +1,14 @@ +package scala { + + module a { + + module b { + + trait c {} + def foo(x: c): c = bar(x) + + } + + def bar(x: b.c): a.b.c = x + } +} diff --git a/test/pos/moduletrans.scala b/test/pos/moduletrans.scala new file mode 100644 index 0000000000..953ce4ee6c --- /dev/null +++ b/test/pos/moduletrans.scala @@ -0,0 +1,8 @@ +module m1 { + + class m() { + def f() = 5 + } + final val m: m = new m() + +} diff --git a/test/pos/nested.scala b/test/pos/nested.scala new file mode 100644 index 0000000000..e81b710d72 --- /dev/null +++ b/test/pos/nested.scala @@ -0,0 +1,27 @@ +// A non-trivial example of nested classes (mostly to test +// ExplicitOuterClasses). + +class A(pa : Int) { + def a1 = pa; + class B(pb : Int) { + def b1 = pa+pb+a1; + class C(pc : Int) extends A(b1) { + def c1 = pc+pb+pa + } + val c1 = new C(66) + } +} + +class M(x : Int) { + def m1 = x +} + +class A1(x : Int) extends A(x) with M(x) { + class D() extends B(42) { + val c2 = new C(66); + class E() extends C(5) { + def e1 = c1+b1+a1; + def e2 = new D(); + } + } +} diff --git a/test/pos/override.scala b/test/pos/override.scala new file mode 100644 index 0000000000..d930986913 --- /dev/null +++ b/test/pos/override.scala @@ -0,0 +1,11 @@ +trait A extends Object { + def f = 1 +} + +trait B extends Object { + def f = 2 +} + +trait C extends Object with A with B { + override def f = super.f +} diff --git a/test/pos/patterns.scala b/test/pos/patterns.scala new file mode 100644 index 0000000000..671ba096e3 --- /dev/null +++ b/test/pos/patterns.scala @@ -0,0 +1,13 @@ +trait Option[a] {} +case class Some[a](x: a) extends Option[a]; +case class None[a]() extends Option[a]; + +module test { + + def println(str: String): Unit = java.lang.System.out.println(str); + + def print(opt: Option[String]) = opt match { + case Some(x) => println(x); + case None() => println("nothing"); + } +} \ No newline at end of file diff --git a/test/pos/patterns2.scala b/test/pos/patterns2.scala new file mode 100644 index 0000000000..d10053bc01 --- /dev/null +++ b/test/pos/patterns2.scala @@ -0,0 +1,16 @@ +trait Option {} +case class Choice(a: Option, b: Option) extends Option; +case class Some(x: java.lang.String) extends Option; +case class None() extends Option; + +module test { + + def f(opt: Option) = opt match { + case Choice(Some("one"), Some(x)) => 1; + case Choice(Some("two"), None()) => 1; + case Choice(y, Some("two")) => 2; + case Choice(Some(z), a) => 3; + case Some(b) => 4; + case None() => 5; + } +} \ No newline at end of file diff --git a/test/pos/philippe1.scala b/test/pos/philippe1.scala new file mode 100644 index 0000000000..d079c5974a --- /dev/null +++ b/test/pos/philippe1.scala @@ -0,0 +1,8 @@ +module test { + def id[a](xs: Array[a]): Array[a] = xs; + + def main(args: Array[String]): Unit = { + val res: Array[String] = id(args); + () + } +} \ No newline at end of file diff --git a/test/pos/philippe2.scala b/test/pos/philippe2.scala new file mode 100644 index 0000000000..2f61fc46ca --- /dev/null +++ b/test/pos/philippe2.scala @@ -0,0 +1,5 @@ +class m1() { + def n() = 0; + def foo(i: Int)(j: Int): Unit = (); + val bar = foo(n()); +} diff --git a/test/pos/reftest.scala b/test/pos/reftest.scala new file mode 100644 index 0000000000..27083bd2cc --- /dev/null +++ b/test/pos/reftest.scala @@ -0,0 +1,4 @@ +import scala._; +module test { + val x: Ref[Int] = new Ref(1); +} \ No newline at end of file diff --git a/test/pos/sort1.scala b/test/pos/sort1.scala new file mode 100644 index 0000000000..0a75a87272 --- /dev/null +++ b/test/pos/sort1.scala @@ -0,0 +1,33 @@ +module test { + + type String = java.lang.String; + + def while(def c: Boolean)(def b: Unit): Unit = + if (c) { b ; while(c)(b) } + else (); + + def sort(a: Array[Double]): Unit = { + + def swap(i: Int, j: Int): Unit = { + val t = a(i) ; val u = a.apply(j) ; a(i) = u ; a(j) = t + } + + def sort1(l: Int, r: Int): Unit = { + val pivot = a((l + r) / 2); + var i = l, j = r; + while (i <= j) { + while (a(i) < pivot) { i = i + 1 } + while (a(j) > pivot) { j = j - 1 } + if (i <= j) { + swap(i, j); + i = i + 1; + j = j - 1; + } + } + if (l < j) sort1(l, j); + if (j < r) sort1(i, r); + } + + sort1(0, a.length - 1); + } +} \ No newline at end of file diff --git a/test/pos/sqrt.scala b/test/pos/sqrt.scala new file mode 100644 index 0000000000..f6a53824b4 --- /dev/null +++ b/test/pos/sqrt.scala @@ -0,0 +1,21 @@ +module test { + + def abs(x: Double) = if (x >= 0) x else 0 - x; + + + def square(x: Double) = x * x; + + def sqrt(x: Double) = { + def sqrtIter(guess: Double): Double = + if (isGoodEnough(guess)) guess + else sqrtIter(improve(guess)); + + def improve(guess: Double) = + (guess + x / guess) / 2; + + def isGoodEnough(guess: Double) = + abs(square(guess) - x) < 0.001; + + sqrtIter(1.0); + } +} \ No newline at end of file diff --git a/test/pos/stable.scala b/test/pos/stable.scala new file mode 100644 index 0000000000..3bbcad3799 --- /dev/null +++ b/test/pos/stable.scala @@ -0,0 +1,11 @@ +trait Base { + val x: Int; + val y: Int; + var z: Int; +} + +class Sub() extends Base with { + override val x: Int = 1; + override val y: Int = 2; + override var z: Int = 3; +} diff --git a/test/pos/strings.scala b/test/pos/strings.scala new file mode 100644 index 0000000000..75bf23f5e3 --- /dev/null +++ b/test/pos/strings.scala @@ -0,0 +1,6 @@ +// martin 1-3-2002: it seems there is a problem with the way Serializable is loaded. +module test { + + def f() = "hello".concat("world"); + +} diff --git a/test/pos/test1.scala b/test/pos/test1.scala new file mode 100644 index 0000000000..6e8e4f98a7 --- /dev/null +++ b/test/pos/test1.scala @@ -0,0 +1,5 @@ +module test { + + def f() = 5; + +} diff --git a/test/pos/test2.scala b/test/pos/test2.scala new file mode 100644 index 0000000000..4c73e0a10b --- /dev/null +++ b/test/pos/test2.scala @@ -0,0 +1,5 @@ +import scala._; +module test2 { + def f(x: Int): Int = 'a'; + def g(x: Int) = f(f(x)); +} \ No newline at end of file diff --git a/test/pos/test4.scala b/test/pos/test4.scala new file mode 100644 index 0000000000..c71b65a283 --- /dev/null +++ b/test/pos/test4.scala @@ -0,0 +1,45 @@ +trait C {} +trait D {} +trait E {} + +module test { + def c: C = c; + def d: D = d; + def e: E = e; +} + +import test._; + +trait S extends o.I[D] with { + def bar: E = foo(c,d); +} + +class O[X]() { + trait I[Y] { + def foo(x: X, y: Y): E = e; + } + val i:I[E] = null; + val j:I[X] = null; +} + +module o extends O[C]() { + + def main = { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar + } +} + +class Main() { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar; +} + diff --git a/test/pos/test4a.scala b/test/pos/test4a.scala new file mode 100644 index 0000000000..0d653c5012 --- /dev/null +++ b/test/pos/test4a.scala @@ -0,0 +1,16 @@ +trait C {} + +class O[X]() { + trait I[Y] { + def foo(y: Y): Y = y; + } + val j:I[X] = null; +} + +module o extends O[C]() { + def c: C = c; + def main = { + o.j.foo(c); + } +} + diff --git a/test/pos/test4refine.scala b/test/pos/test4refine.scala new file mode 100644 index 0000000000..8e3fee70cc --- /dev/null +++ b/test/pos/test4refine.scala @@ -0,0 +1,49 @@ +trait C {} +trait D {} +trait E {} + +module test { + def c: C = c; + def d: D = d; + def e: E = e; +} + +import test._; + +trait S extends o.I { + type Y = D; + def bar: E = foo(c,d); +} + +abstract class O() { + type X; + abstract trait I { + type Y; + def foo(x: X, y: Y): E = e; + } + val i:I with { type Y = E } = null; + val j:I with { type Y = X } = null; +} + +module o extends O() { + type X = C; + + def main = { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar + } +} + +class Main() { + val s: S = null; + import s._; + foo(c,d); + o.i.foo(c,e); + o.j.foo(c,c); + bar; +} + diff --git a/test/pos/test5.scala b/test/pos/test5.scala new file mode 100644 index 0000000000..4851e5e6a2 --- /dev/null +++ b/test/pos/test5.scala @@ -0,0 +1,68 @@ +import scala._; + +module test { + + trait F[If] {} + + def f[Jf](h: Jf):F[Jf] = f@[Jf](h); + + trait G[Ig] {} + + def g[Jg](h: Jg):G[Jg] = g@[Jg](h); + + class M[P]() { + class I[X]() { + // Methods to check the type X and P as seen from instances of I + def chk_ix(x: X): Unit = (); + def chk_ip(p: P): Unit {} + + // Value with type X as seen from instances of I + def val_ix: X = val_ix; + } + + val i:I[G[P]] = null; + + // Values with types P and i.X as seen from instances of M + def val_mp: P = val_mp; + def val_mix: G[P] = g@[P](val_mp); + } + + class N[Q]() extends M[F[Q]]() { + val j:J[G[Q]] = null; + + class J[Y]() extends I[G[Y]]() { + // Values with types Y and X as seen from instances of J + def val_jy: Y = val_jy; + def val_jx: G[Y] = g@[Y](val_jy); + + // Check type P + chk_ip(val_mp); + chk_ip(val_np); + } + + // Values with types Q, X.P, i.X, j.Y and j.X as seen from instances of N + def val_nq: Q = val_nq; + def val_np: F[Q] = f@[Q](val_nq); + def val_nix: G[F[Q]] = g@[F[Q]](val_np); + def val_njy: G[Q] = g@[Q](val_nq); + def val_njx: G[G[Q]] = g@[G[Q]](val_njy); + + // Check type i.P + i.chk_ip(val_mp); + i.chk_ip(val_np); + + // Check type j.P + j.chk_ip(val_mp); + j.chk_ip(val_np); + + // Check type i.X + i.chk_ix(i.val_ix); + i.chk_ix(val_mix); + i.chk_ix(val_nix); + + // Check j.X + j.chk_ix(j.val_ix); + j.chk_ix(j.val_jx); + j.chk_ix(val_njx); + } +} \ No newline at end of file diff --git a/test/pos/test5refine.scala b/test/pos/test5refine.scala new file mode 100644 index 0000000000..1097c6cff7 --- /dev/null +++ b/test/pos/test5refine.scala @@ -0,0 +1,75 @@ +import scala._; + +module test { + + abstract trait F { type If; } + + def f[Jf](h: Jf):F with { type If = Jf } = f@[Jf](h); + + abstract trait G { type Ig; } + + def g[Jg](h: Jg):G with { type Ig = Jg } = g@[Jg](h); + + abstract class M() { + type P; + abstract class I() { + type X; + + // Methods to check the type X and P as seen from instances of I + def chk_ix(x: X): Unit = {} + def chk_ip(p: P): Unit = {} + + // Value with type X as seen from instances of I + def val_ix: X = val_ix; + } + + val i: I with { type X = G with { type Ig = P } } = null; + + // Values with types P and i.X as seen from instances of M + def val_mp: P = val_mp; + def val_mix: G with { type Ig = P } = g@[P](val_mp); + } + + abstract class N() extends M() { + type Q; + type P = F with { type If = Q }; + val j:J with { type Y = G with { type Ig = Q } } = null; + + abstract class J() extends I() { + type Y; + type X = G with { type Ig = Y; }; + // Values with types Y and X as seen from instances of J + def val_jy: Y = val_jy; + def val_jx: G with { type Ig = Y; } = g@[Y](val_jy); + + // Check type P + chk_ip(val_mp); + chk_ip(val_np); + } + + // Values with types Q, X.P, i.X, j.Y and j.X as seen from instances of N + def val_nq: Q = val_nq; + def val_np: F with { type If = Q } = f@[Q](val_nq); + def val_nix: G with { type Ig = F with { type If = Q } } = g@[F with { type If = Q }](val_np); + def val_njy: G with { type Ig = Q; } = g@[Q](val_nq); + def val_njx: G with { type Ig = G with { type Ig = Q }} = g@[G with { type Ig = Q; }](val_njy); + + // Check type i.P + i.chk_ip(val_mp); + i.chk_ip(val_np); + + // Check type j.P + j.chk_ip(val_mp); + j.chk_ip(val_np); + + // Check type i.X + i.chk_ix(i.val_ix); + i.chk_ix(val_mix); + i.chk_ix(val_nix); + + // Check j.X + j.chk_ix(j.val_ix); + j.chk_ix(j.val_jx); + j.chk_ix(val_njx); + } +} \ No newline at end of file -- cgit v1.2.3