Skip to content
Snippets Groups Projects
Commit 2eda3724 authored by Philippe Suter's avatar Philippe Suter
Browse files

Better representation for And(seq) and Or(seq).

parent 07e55590
Branches
Tags
No related merge requests found
...@@ -181,8 +181,11 @@ object Trees { ...@@ -181,8 +181,11 @@ object Trees {
case _ => new And(Seq(l,r)) case _ => new And(Seq(l,r))
} }
def apply(exprs: Seq[Expr]) : Expr = { def apply(exprs: Seq[Expr]) : Expr = {
val simpler = exprs.filter(_ != BooleanLiteral(true)) val simpler = exprs.flatMap(_ match {
if(simpler.isEmpty) BooleanLiteral(true) else simpler.reduceRight(And(_,_)) case And(es) => es
case o => Seq(o)
}).takeWhile(_ != BooleanLiteral(false)).filter(_ != BooleanLiteral(true))
if(simpler.isEmpty) BooleanLiteral(true) else new And(simpler)
} }
def unapply(and: And) : Option[Seq[Expr]] = def unapply(and: And) : Option[Seq[Expr]] =
...@@ -202,13 +205,18 @@ object Trees { ...@@ -202,13 +205,18 @@ object Trees {
object Or { object Or {
def apply(l: Expr, r: Expr) : Expr = (l,r) match { def apply(l: Expr, r: Expr) : Expr = (l,r) match {
case (BooleanLiteral(true),_) => BooleanLiteral(true)
case (BooleanLiteral(false),_) => r case (BooleanLiteral(false),_) => r
case (_,BooleanLiteral(false)) => l case (_,BooleanLiteral(false)) => l
case _ => new Or(Seq(l,r)) case _ => new Or(Seq(l,r))
} }
def apply(exprs: Seq[Expr]) : Expr = { def apply(exprs: Seq[Expr]) : Expr = {
val simpler = exprs.filter(_ != BooleanLiteral(false)) val simpler = exprs.flatMap(_ match {
if(simpler.isEmpty) BooleanLiteral(false) else simpler.reduceRight(Or(_,_)) case Or(es) => es
case o => Seq(o)
}).takeWhile(_ != BooleanLiteral(true)).filter(_ != BooleanLiteral(false))
if(simpler.isEmpty) BooleanLiteral(false) else new Or(simpler)
} }
def unapply(or: Or) : Option[Seq[Expr]] = def unapply(or: Or) : Option[Seq[Expr]] =
......
...@@ -107,21 +107,17 @@ object ArithmeticNormalization { ...@@ -107,21 +107,17 @@ object ArithmeticNormalization {
simplePostTransform(simplify0)(expr) simplePostTransform(simplify0)(expr)
} }
//assume the formula consist only of top level AND, find a top level // Assume the formula consist only of top level AND, find a top level
//Equals and extract it, return the remaining formula as well // Equals and extract it, return the remaining formula as well
//warning: also assume that And are always binary !
def extractEquals(expr: Expr): (Option[Equals], Expr) = expr match { def extractEquals(expr: Expr): (Option[Equals], Expr) = expr match {
case And(Seq(eq@Equals(_, _), f)) => (Some(eq), f) case And(es) =>
case And(Seq(f, eq@Equals(_, _))) => (Some(eq), f) // OK now I'm just messing with you.
case And(Seq(f1, f2)) => extractEquals(f1) match { val (r, nes) = es.foldLeft[(Option[Equals],Seq[Expr])]((None, Seq())) {
case (Some(eq), r) => (Some(eq), And(r, f2)) case ((None, nes), eq @ Equals(_,_)) => (Some(eq), nes)
case (None, r1) => { case ((o, nes), e) => (o, e +: nes)
val (eq, r2) = extractEquals(f2)
(eq, And(r1, r2))
} }
} (r, And(nes.reverse))
case e => (None, e) case e => (None, e)
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment