@@ -113,16 +113,17 @@ trait SpaceLogic {
113
113
/** Display space in string format */
114
114
def show (sp : Space ): String
115
115
116
- /** Simplify space using the laws, there's no nested union after simplify */
116
+ /** Simplify space such that a space equal to `Empty` becomes `Empty` */
117
117
def simplify (space : Space )(using Context ): Space = trace(s " simplify ${show(space)} --> " , debug, x => show(x.asInstanceOf [Space ]))(space match {
118
118
case Prod (tp, fun, spaces) =>
119
- val sp = Prod (tp, fun, spaces.map(simplify(_) ))
120
- if (sp.params .contains(Empty )) Empty
119
+ val sps = spaces.map(simplify(_))
120
+ if (sps .contains(Empty )) Empty
121
121
else if (canDecompose(tp) && decompose(tp).isEmpty) Empty
122
- else sp
122
+ else Prod (tp, fun, sps)
123
123
case Or (spaces) =>
124
124
val spaces2 = spaces.map(simplify(_)).filter(_ != Empty )
125
125
if spaces2.isEmpty then Empty
126
+ else if spaces2.lengthCompare(1 ) == 0 then spaces2.head
126
127
else Or (spaces2)
127
128
case Typ (tp, _) =>
128
129
if (canDecompose(tp) && decompose(tp).isEmpty) Empty
@@ -243,10 +244,10 @@ trait SpaceLogic {
243
244
tryDecompose1(tp1)
244
245
else
245
246
a
246
- case (_, Or (ss)) =>
247
- ss.foldLeft(a)(minus)
248
247
case (Or (ss), _) =>
249
248
Or (ss.map(minus(_, b)))
249
+ case (_, Or (ss)) =>
250
+ ss.foldLeft(a)(minus)
250
251
case (Prod (tp1, fun, ss), Typ (tp2, _)) =>
251
252
// uncovered corner case: tp2 :< tp1, may happen when inheriting case class
252
253
if (isSubType(tp1, tp2))
@@ -272,7 +273,10 @@ trait SpaceLogic {
272
273
else if cache.forall(sub => isSubspace(sub, Empty )) then Empty
273
274
else
274
275
// `(_, _, _) - (Some, None, _)` becomes `(None, _, _) | (_, Some, _) | (_, _, Empty)`
275
- Or (LazyList (range : _* ).map { i => Prod (tp1, fun1, ss1.updated(i, sub(i))) })
276
+ val spaces = LazyList (range : _* ).flatMap { i =>
277
+ flatten(sub(i)).map(s => Prod (tp1, fun1, ss1.updated(i, s)))
278
+ }
279
+ Or (spaces)
276
280
}
277
281
}
278
282
}
0 commit comments