Skip to content

Commit 848152b

Browse files
committed
Space: Refine isSubspace property & an example
1 parent bea0935 commit 848152b

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ trait SpaceLogic {
164164
List(space)
165165
}
166166

167-
/** Is `a` a subspace of `b`? Equivalent to `a - b == Empty`, but faster */
167+
/** Is `a` a subspace of `b`? Equivalent to `simplify(simplify(a) - simplify(b)) == Empty`, but faster */
168168
def isSubspace(a: Space, b: Space)(using Context): Boolean = trace(s"isSubspace(${show(a)}, ${show(b)})", debug) {
169169
def tryDecompose1(tp: Type) = canDecompose(tp) && isSubspace(Or(decompose(tp)), b)
170170
def tryDecompose2(tp: Type) = canDecompose(tp) && isSubspace(a, Or(decompose(tp)))
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package dotty.tools
2+
package dotc
3+
package transform
4+
package patmat
5+
6+
import core.*, Annotations.*, Contexts.*, Decorators.*, Flags.*, Names.*, StdNames.*, Symbols.*, Types.*
7+
import ast.*, tpd.*
8+
9+
import vulpix.TestConfiguration, TestConfiguration.basicClasspath
10+
11+
import org.junit, junit.Test, junit.Assert.*
12+
13+
class SpaceEngineTest:
14+
@Test def isSubspaceTest1: Unit = inCompilerContext(basicClasspath) {
15+
// Testing the property of `isSubspace` that:
16+
// isSubspace(a, b) <=> simplify(simplify(a) - simplify(a)) == Empty
17+
// Previously there were no simplify calls,
18+
// and this is a counter-example,
19+
// for which you need either to simplify(b) or simplify the minus result.
20+
val engine = patmat.SpaceEngine()
21+
import engine.*
22+
23+
val tp = defn.ConsClass.typeRef.appliedTo(defn.AnyType)
24+
val unappTp = requiredMethod("scala.collection.immutable.::.unapply").termRef
25+
val params = List(Empty, Typ(tp))
26+
27+
val a = Prod(tp, unappTp, params)
28+
val b = Empty
29+
30+
val res1 = isSubspace(a, b)
31+
32+
val a2 = simplify(a)
33+
val b2 = simplify(b)
34+
val rem1 = minus(a2, b2)
35+
val rem2 = simplify(rem1)
36+
val res2 = rem2 == Empty
37+
38+
assertEquals(
39+
i"""|isSubspace:
40+
|
41+
|isSubspace(a, b) = $res1
42+
|
43+
|Should be equivalent to:
44+
|simplify(simplify(a) - simplify(b)) == Empty
45+
|simplify(a2 - b2) == Empty
46+
|simplify(rem1) == Empty
47+
|rem2 == Empty
48+
|
49+
|a = ${show(a)}
50+
|b = ${show(b)}
51+
|a2 = ${show(a2)}
52+
|b2 = ${show(b2)}
53+
|rem1 = ${show(rem1)}
54+
|rem2 = ${show(rem2)}
55+
|
56+
|a = ${a.toString}
57+
|b = ${b.toString}
58+
|a2 = ${a2.toString}
59+
|b2 = ${b2.toString}
60+
|rem1 = ${rem1.toString}
61+
|rem2 = ${rem2.toString}
62+
|
63+
|""".stripMargin, res1, res2)
64+
}

0 commit comments

Comments
 (0)