Commit c6bc842d authored by Arthur Bit-Monnot's avatar Arthur Bit-Monnot

Abandon IArray for a new Vec class that uses specialization in a single class.

parent 3ea2300a
......@@ -34,8 +34,14 @@ lazy val commonSettings = Seq(
"-language:higherKinds",
"-language:existentials",
// experimental option to speed up the build
//"-Ycache-plugin-class-loader",
//"-Ycache-macro-class-loader"
"-Ycache-plugin-class-loader:last-modified",
"-Ycache-macro-class-loader:last-modified",
// "-opt:simplify-jumps",
// "-opt:compact-locals",
// "-opt:copy-propagation",
// "-opt:box-unbox",
// "-opt:closure-invocations",
// "-opt:unreachable-code"
),
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.6"),
//addCompilerPlugin("io.tryp" % "splain" % "0.2.10" cross CrossVersion.patch),
......
package dahu.model.compiler
import dahu.IArray
import dahu.utils.Vec
import dahu.maps.ArrayMap
import dahu.model.input._
import dahu.model.ir._
......@@ -17,7 +17,7 @@ object Algebras {
case x @ Cst(value) => CstF(Value(value), x.typ)
case x: Computation[_] => ComputationF(x.f, x.args, x.typ)
case x @ SubjectTo(value, cond) => Partial(value, cond, x.typ)
case x @ Product(value) => ProductF(x.members.asInstanceOf[IArray[Tentative[_]]], x.typ)
case x @ Product(value) => ProductF(x.members.asInstanceOf[Vec[Tentative[_]]], x.typ)
case x @ Optional(value, present) => OptionalF(value, present, x.typ)
case x @ ITE(cond, onTrue, onFalse) => ITEF(cond, onTrue, onFalse, x.typ)
case Present(partial) => PresentF(partial)
......
package dahu.model.compiler
import dahu.IArray
import dahu.utils.Vec
import dahu.utils.Vec._
import dahu.model.ir.{ComputationF, CstF, ExprF, Total}
import dahu.model.math._
import dahu.model.types._
import dahu.recursion._
import dahu.utils.errors._
import dahu.IArray._
import scala.collection.mutable.ArrayBuffer
......@@ -46,7 +46,7 @@ object Optimizations {
case ((vs, cs), CstF(v, _)) => (vs, v :: cs)
case ((vs, cs), x) => (x :: vs, cs)
}
val evalOfConstants = CstF[Fix[Total]](Value(f.compute(IArray.fromSeq(csts))), f.tpe)
val evalOfConstants = CstF[Fix[Total]](Value(f.compute(Vec.fromSeq(csts))), f.tpe)
if(vars.isEmpty) {
// no unevaluated terms, return results
evalOfConstants
......@@ -81,8 +81,8 @@ object Optimizations {
}
val elimSingletonAndOr: PASS = namedPass("elim-singleton-and-or") {
case ComputationF(bool.And, Arr1(arg), _) => arg.unfix
case ComputationF(bool.Or, Arr1(arg), _) => arg.unfix
case ComputationF(bool.And, Vec1(arg), _) => arg.unfix
case ComputationF(bool.Or, Vec1(arg), _) => arg.unfix
case x => x
}
......@@ -93,8 +93,8 @@ object Optimizations {
}
val elimTautologies: PASS = namedPass("elim-tautologies") {
case ComputationF(int.LEQ, Arr2(a1, a2), _) if a1 == a2 => TRUE.unfix
case ComputationF(int.EQ, Arr2(a1, a2), _) if a1 == a2 => TRUE.unfix
case ComputationF(int.LEQ, Vec2(a1, a2), _) if a1 == a2 => TRUE.unfix
case ComputationF(int.EQ, Vec2(a1, a2), _) if a1 == a2 => TRUE.unfix
case x => x
}
}
......
package dahu.model.functions
import dahu.IArray
import dahu.utils._
import dahu.model.types.Value
import dahu.model.types._
abstract class Fun[O: Tag] {
final val outType: Tag[O] = Tag[O]
def compute(args: IArray[Value]): O
def compute(args: Vec[Value]): O
def name: String
......@@ -16,7 +16,7 @@ abstract class Fun[O: Tag] {
abstract class Fun1[-I: Tag, O: Tag] extends Fun[O] {
final val inType = typeOf[I]
override final def compute(args: IArray[Value]): O = {
override final def compute(args: Vec[Value]): O = {
require(args.size == 1)
of(args(0).asInstanceOf[I])
}
......@@ -34,7 +34,7 @@ abstract class Fun2[-I1: Tag, -I2: Tag, O: Tag] extends Fun[O] {
final val inType1 = typeOf[I1]
final val inType2 = typeOf[I2]
override final def compute(args: IArray[Value]): O = {
override final def compute(args: Vec[Value]): O = {
require(args.size == 2, "Wrong number of arguments, expected 2")
of(args(0).asInstanceOf[I1], args(1).asInstanceOf[I2])
}
......@@ -47,7 +47,7 @@ abstract class Fun3[-I1: Tag, -I2: Tag, -I3: Tag, O: Tag] extends Fun[O] {
final val inType2 = typeOf[I2]
final val inType3 = typeOf[I3]
override final def compute(args: IArray[Value]): O = {
override final def compute(args: Vec[Value]): O = {
require(args.size == 3, "Wrong number of arguments, expected 3")
of(args(0).asInstanceOf[I1], args(1).asInstanceOf[I2], args(2).asInstanceOf[I3])
}
......@@ -61,7 +61,7 @@ abstract class Fun4[-I1: Tag, -I2: Tag, -I3: Tag, -I4: Tag, O: Tag] extends Fun[
final val inType3 = typeOf[I3]
final val inType4 = typeOf[I4]
override final def compute(args: IArray[Value]): O = {
override final def compute(args: Vec[Value]): O = {
require(args.size == 4, "Wrong number of arguments, expected 3")
of(args(0).asInstanceOf[I1],
args(1).asInstanceOf[I2],
......@@ -75,7 +75,7 @@ abstract class Fun4[-I1: Tag, -I2: Tag, -I3: Tag, -I4: Tag, O: Tag] extends Fun[
abstract class FunN[-I: Tag, O: Tag] extends Fun[O] {
final val inTypes = typeOf[I]
override final def compute(args: IArray[Value]): O =
override final def compute(args: Vec[Value]): O =
of(args.toSeq.asInstanceOf[Seq[I]]) //TODO: avoid conversion
def of(args: Seq[I]): O //TODO
......
package dahu.model.input
import cats.Id
import dahu.IArray
import dahu.utils._
import dahu.graphs.DAG
import dahu.model.functions._
import dahu.model.types._
......@@ -82,9 +82,9 @@ sealed abstract class Computation[O] extends Tentative[O] {
final case class Product[T[_[_]]](value: T[Tentative])(implicit tt: ProductTag[T])
extends Tentative[T[Id]] {
override def typ: ProductTag[T] = tt
def members: IArray[Tentative[Any]] = tt.exprProd.extractTerms(value)
def buildFromVals(terms: IArray[Any]): T[Id] = tt.idProd.buildFromTerms(terms)
def buildFromExpr(terms: IArray[Tentative[Any]]): T[Tentative] = tt.exprProd.buildFromTerms(terms)
def members: Vec[Tentative[Any]] = tt.exprProd.extractTerms(value)
def buildFromVals(terms: Vec[Any]): T[Id] = tt.idProd.buildFromTerms(terms)
def buildFromExpr(terms: Vec[Tentative[Any]]): T[Tentative] = tt.exprProd.buildFromTerms(terms)
}
object Product {
def fromSeq[T](seq: Seq[Tentative[T]])(implicit ev: ProductTag[ProductTag.Sequence[?[_], T]])
......@@ -95,30 +95,30 @@ object Product {
def fromMap[K: ClassTag, V: ClassTag](map: Map[K, Tentative[V]])(
implicit tt: universe.WeakTypeTag[Map[K, Id[V]]]): Product[PMap[K, ?[_], V]] = {
type M[F[_]] = PMap[K, F, V]
val keys: IArray[K] = IArray.fromSeq(map.keys.toSeq)
val values: IArray[Tentative[Any]] = keys.map(map(_).asInstanceOf[Tentative[Any]])
val keys: Vec[K] = Vec.fromSeq(map.keys.toSeq)
val values: Vec[Tentative[Any]] = keys.map(map(_).asInstanceOf[Tentative[Any]])
// build a specific type tag that remembers the keys of the original map.
val tag = new ProductTag[M] {
override def exprProd: ProductExpr[M, Tentative] = new ProductExpr[M, Tentative] {
override def extractTerms(prod: M[Tentative])(
implicit ct: ClassTag[Tentative[Any]]): IArray[Tentative[Any]] = {
implicit ct: ClassTag[Tentative[Any]]): Vec[Tentative[Any]] = {
assert(prod == map)
values
}
override def buildFromTerms(terms: IArray[Tentative[Any]]): M[Tentative] = {
override def buildFromTerms(terms: Vec[Tentative[Any]]): M[Tentative] = {
assert(terms == values)
map
}
}
override def idProd: ProductExpr[M, Id] = new ProductExpr[M, Id] {
override def extractTerms(prod: M[Id])(implicit ct: ClassTag[Id[Any]]): IArray[Id[Any]] = {
override def extractTerms(prod: M[Id])(implicit ct: ClassTag[Id[Any]]): Vec[Id[Any]] = {
assert(prod.keys == map.keys)
keys.map(k => prod(k))
}
override def buildFromTerms(terms: IArray[Id[Any]]): M[Id] = {
override def buildFromTerms(terms: Vec[Id[Any]]): M[Id] = {
assert(terms.size == values.size)
keys.zip(terms.map(_.asInstanceOf[Id[V]])).toMap
}
......@@ -134,10 +134,10 @@ object Product {
}
trait ProductExpr[P[_[_]], F[_]] {
def extractTerms(prod: P[F])(implicit ct: ClassTag[F[Any]]): IArray[F[Any]]
def buildFromTerms(terms: IArray[F[Any]]): P[F]
def buildFromValues(terms: IArray[F[Value]]): P[F] =
buildFromTerms(terms.asInstanceOf[IArray[F[Any]]])
def extractTerms(prod: P[F])(implicit ct: ClassTag[F[Any]]): Vec[F[Any]]
def buildFromTerms(terms: Vec[F[Any]]): P[F]
def buildFromValues(terms: Vec[F[Value]]): P[F] =
buildFromTerms(terms.asInstanceOf[Vec[F[Any]]])
}
object ProductExpr {
......@@ -154,9 +154,9 @@ object ProductExpr {
implicit def genPE[P[_[_]], F[_], H <: HList](implicit gen: Generic.Aux[P[F], H],
hListExtract: HListExtract[H, F]) =
new ProductExpr[P, F] {
override def extractTerms(prod: P[F])(implicit ct: ClassTag[F[Any]]): IArray[F[Any]] =
IArray.fromSeq(hListExtract.terms(gen.to(prod)))
override def buildFromTerms(terms: IArray[F[Any]]): P[F] =
override def extractTerms(prod: P[F])(implicit ct: ClassTag[F[Any]]): Vec[F[Any]] =
Vec.fromSeq(hListExtract.terms(gen.to(prod)))
override def buildFromTerms(terms: Vec[F[Any]]): P[F] =
gen.from(hListExtract.fromTerms(terms.toSeq))
}
......
......@@ -3,7 +3,8 @@ package dahu.model.interpreter
import cats.Foldable
import cats.implicits._
import cats.kernel.Monoid
import dahu.IArray
import dahu.utils._
import dahu.model.input.Present
import dahu.model.ir._
import dahu.model.types._
......@@ -66,7 +67,7 @@ object Interpreter {
}
object Result {
def pure[A](a: A): Result[A] = Res(a)
def sequence[T: ClassTag](rs: IArray[Result[T]]): Result[IArray[T]] = {
def sequence[T: ClassTag](rs: Vec[Result[T]]): Result[Vec[T]] = {
val l = rs.toList
@tailrec def go(current: Result[List[T]], pending: List[Result[T]]): Result[List[T]] = {
pending match {
......@@ -84,7 +85,7 @@ object Interpreter {
}
}
val res = go(pure(Nil), l)
res.map(IArray.fromSeq(_))
res.map(Vec.fromSeq(_))
}
implicit def monoidInstance[T: Monoid](): Monoid[Result[T]] = new Monoid[Result[T]] {
override def empty: Result[T] = Res(Monoid[T].empty)
......
package dahu.model.ir
import dahu.{IArray, SFunctor}
import dahu.utils._
import dahu.model.functions.Fun
import dahu.model.input.Ident
import dahu.model.types.{ProductTag, Tag, Type, Value}
......@@ -72,7 +72,7 @@ object Total {
case x @ InputF(_, _) => x
case x @ CstF(_, _) => x
case ComputationF(fun, args, typ) =>
new ComputationF(fun, implicitly[SFunctor[IArray]].smap(args)(f), typ)
new ComputationF(fun, implicitly[SFunctor[Vec]].smap(args)(f), typ)
case ProductF(members, typ) => ProductF(members.map(f), typ)
case ITEF(cond, onTrue, onFalse, typ) => ITEF(f(cond), f(onTrue), f(onFalse), typ)
}
......@@ -103,22 +103,21 @@ object CstF {
implicit def typeParamConversion[F, G](fa: CstF[F]): CstF[G] = fa.asInstanceOf[CstF[G]]
}
final case class ComputationF[@sp(Int) F](fun: Fun[_], args: IArray[F], typ: Type)
extends Total[F] {
final case class ComputationF[@sp(Int) F](fun: Fun[_], args: Vec[F], typ: Type) extends Total[F] {
override def toString: String = s"$fun(${args.mkString(", ")})"
}
object ComputationF {
def apply[F: ClassTag](fun: Fun[_], args: Seq[F], tpe: Type): ComputationF[F] =
new ComputationF(fun, IArray.fromArray(args.toArray), tpe)
new ComputationF(fun, Vec.fromArray(args.toArray), tpe)
}
final case class ProductF[@sp(Int) F](members: IArray[F], typ: ProductTag[Any]) extends Total[F] {
final case class ProductF[@sp(Int) F](members: Vec[F], typ: ProductTag[Any]) extends Total[F] {
override def toString: String = members.mkString("(", ", ", ")")
}
object ProductF {
def apply[F: ClassTag](args: Seq[F], tpe: ProductTag[Any]): ProductF[F] =
new ProductF[F](IArray.fromArray(args.toArray), tpe)
new ProductF[F](Vec.fromArray(args.toArray), tpe)
}
final case class ITEF[@sp(Int) F](cond: F, onTrue: F, onFalse: F, typ: Type) extends Total[F] {
......
......@@ -2,8 +2,7 @@ package dahu.model.problem
import cats.Functor
import cats.implicits._
import dahu.SFunctor
import dahu.IArray.Arr1
import dahu._
import dahu.model.functions._
import dahu.model.input.Anonymous
import dahu.model.ir._
......@@ -11,6 +10,8 @@ import dahu.model.math._
import dahu.model.math.obj.Unboxed
import dahu.model.problem.SatisfactionProblem.{ILazyTree, RootedLazyTree, TreeNode}
import dahu.model.types._
import dahu.utils.SFunctor
import dahu.utils.Vec.Vec1
import dahu.utils.errors._
import scala.collection.mutable
......@@ -158,7 +159,7 @@ class IntBoolSatisfactionProblem[X](val ast: RootedLazyTree[X, Total, cats.Id])
if supportedFunctions.contains(wf.f) && args.forall(x => sup(prev(x))) =>
TRANS(rec)(prev)(ComputationF(wf.f, args, t)) // unwrap and retry
case x @ ComputationF(f: Unboxed[_], Arr1(arg), t) =>
case x @ ComputationF(f: Unboxed[_], Vec1(arg), t) =>
prev(arg) // unbox operation, use the previous cell
case x =>
......
......@@ -2,11 +2,13 @@ package dahu.model.problem
import cats.Functor
import cats.implicits._
import dahu.{IArray, SFunctor}
import dahu.utils._
import dahu.model.ir._
import dahu.model.math.bool
import dahu.model.types._
import dahu.recursion._
import dahu.utils.SFunctor
import scala.collection.mutable
import scala.reflect.ClassTag
......@@ -116,7 +118,7 @@ object SatisfactionProblem {
rec(ComputationF(bool.And, reduced, Tag.ofBoolean))
}
}
def and(conjuncts: IArray[ID]): ID = {
def and(conjuncts: Vec[ID]): ID = {
if(conjuncts.contains(FALSE)) {
FALSE
} else {
......
package dahu.model.types
import cats.Id
import dahu.IArray
import dahu.utils._
import dahu.model.input.{ProductExpr, Tentative}
import scala.annotation.switch
......@@ -74,17 +74,17 @@ object ProductTag {
override def exprProd: ProductExpr[Sequence[?[_], A], Tentative] =
new ProductExpr[Sequence[?[_], A], Tentative] {
override def extractTerms(prod: Sequence[Tentative, A])(
implicit ct: ClassTag[Tentative[Any]]): IArray[Tentative[Any]] =
IArray.fromSeq(prod.map(_.asInstanceOf[Tentative[Any]]))
override def buildFromTerms(terms: IArray[Tentative[Any]]): Sequence[Tentative, A] =
implicit ct: ClassTag[Tentative[Any]]): Vec[Tentative[Any]] =
Vec.fromSeq(prod.map(_.asInstanceOf[Tentative[Any]]))
override def buildFromTerms(terms: Vec[Tentative[Any]]): Sequence[Tentative, A] =
terms.map(_.asInstanceOf[Tentative[A]]).toSeq
}
override def idProd: ProductExpr[Sequence[?[_], A], Id] =
new ProductExpr[Sequence[?[_], A], Id] {
override def extractTerms(prod: Sequence[Id, A])(
implicit ct: ClassTag[Id[Any]]): IArray[Id[Any]] = IArray.fromSeq(prod)
override def buildFromTerms(terms: IArray[Id[Any]]): Sequence[Id, A] =
terms.asInstanceOf[IArray[Id[A]]].toSeq
implicit ct: ClassTag[Id[Any]]): Vec[Id[Any]] = Vec.fromSeq(prod)
override def buildFromTerms(terms: Vec[Id[Any]]): Sequence[Id, A] =
terms.asInstanceOf[Vec[Id[A]]].toSeq
}
override def typ: Tag.Type = tt.tpe
......
package dahu.planner
import dahu.IArray
import dahu.model.compiler.Algebras
import dahu.model.interpreter.Interpreter
import dahu.model.interpreter.Interpreter.Res
......
......@@ -2,7 +2,7 @@ package dahu.recursion
import cats.{Functor, Monad}
import cats.free.Free
import dahu.SFunctor
import dahu.utils.SFunctor
final class FAlgebraOps[F[_], A](private val self: FAlgebra[F, A]) extends AnyVal {
......
package dahu.recursion
import cats.{Monad, Traverse}
import dahu.SFunctor
import dahu.utils.SFunctor
import scala.reflect.ClassTag
......
package dahu.recursion
import cats.{~>, Functor, Monad, Traverse}
import dahu.SFunctor
import dahu.utils.SFunctor
import scala.reflect.ClassTag
......
......@@ -2,7 +2,7 @@ package dahu.recursion
import cats.{~>, Comonad, Eval, Functor, Monad, Traverse}
import cats.free.{Cofree, Free}
import dahu.SFunctor
import dahu.utils.SFunctor
import spire.ClassTag
object RecursionFn {
......
......@@ -2,6 +2,7 @@ package dahu
import cats.{Applicative, Eval, Functor, Traverse}
import cats.free.{Cofree, Free}
import dahu.utils.SFunctor
import scala.language.implicitConversions
import scala.reflect.ClassTag
......
package dahu.constraints
import dahu.IArray
import dahu.utils._
import dahu.constraints.Constraint.Updater
import dahu.constraints.domains._
import dahu.model.types._
......@@ -45,7 +45,7 @@ object Constraint {
private val emptyUpdateSingleton = Array[Inference[Nothing]]()
def emptyUpdate[T <: SubInt] = emptyUpdateSingleton.asInstanceOf[Array[Inference[T]]]
def fromForward[T <: SubInt](id: T, args: IArray[T], prop: ForwardPropagator): Updater[T] = {
def fromForward[T <: SubInt](id: T, args: Vec[T], prop: ForwardPropagator): Updater[T] = {
(csp: CSP[T]) =>
{
val d = prop.propagate(args, csp.dom)
......@@ -57,7 +57,7 @@ object Constraint {
}
}
}
def fromBackward[T <: SubInt](id: T, args: IArray[T], prop: BackwardPropagator): Updater[T] = {
def fromBackward[T <: SubInt](id: T, args: Vec[T], prop: BackwardPropagator): Updater[T] = {
(csp: CSP[T]) =>
{
val doms = prop.propagate(args, id, csp.dom)
......@@ -76,7 +76,7 @@ class CSP[K <: SubInt](params: ArrayMap.Aux[K, IntProblem.Expr], conjunct: Set[K
type Assignment = ArrayMap.Aux[K, Int]
type Var = K
type Vars = IArray[Var]
type Vars = Vec[Var]
val ids: debox.Buffer[Var] = params.domain.toSortedBuffer
def initialDomains(v: Var): IntDomain = params(v).domain
......
......@@ -5,7 +5,7 @@ import dahu.model.math._
import dahu.utils.errors.unexpected
import dahu.constraints.interval._
import BooleanDomain._
import dahu.IArray
import dahu.utils._
import dahu.model.functions._
import dahu.utils.debug._
import spire.syntax.cfor
......@@ -41,7 +41,7 @@ object Propagator {
}
abstract class IntCompatibilityLayer {
def argsFromInt(args: IArray[Int]): IArray[Value]
def argsFromInt(args: Vec[Int]): Vec[Value]
def outToInt(out: Value): Int
}
object IntCompatibleFunc {
......@@ -52,7 +52,7 @@ object IntCompatibleFunc {
(f.inTypes, f.outType) match {
case (it: IsoInt @unchecked, ot: IsoInt @unchecked) =>
Some(new IntCompatibilityLayer {
override def argsFromInt(args: IArray[Int]): IArray[Value] =
override def argsFromInt(args: Vec[Int]): Vec[Value] =
args.map(a => Value(it.fromInt(a)))
override def outToInt(out: Value): Int = ot.toInt(out)
})
......@@ -62,7 +62,7 @@ object IntCompatibleFunc {
(f.inType1, f.inType2, f.outType) match {
case (it1: IsoInt @unchecked, it2: IsoInt @unchecked, ot: IsoInt @unchecked) =>
Some(new IntCompatibilityLayer {
override def argsFromInt(args: IArray[Int]): IArray[Value] = IArray(
override def argsFromInt(args: Vec[Int]): Vec[Value] = Vec(
Value(it1.fromInt(args(0))),
Value(it2.fromInt(args(1)))
)
......@@ -74,8 +74,8 @@ object IntCompatibleFunc {
(f.inType, f.outType) match {
case (it: IsoInt @unchecked, ot: IsoInt @unchecked) =>
Some(new IntCompatibilityLayer {
override def argsFromInt(args: IArray[Int]): IArray[Value] =
IArray(Value(it.fromInt(args(0))))
override def argsFromInt(args: Vec[Int]): Vec[Value] =
Vec(Value(it.fromInt(args(0))))
override def outToInt(out: Value): Int = ot.toInt(out)
})
case _ => None
......@@ -97,7 +97,7 @@ object Types {
}
trait ForwardPropagator {
def propagate[T](args: IArray[T], dom: T => Interval): Interval
def propagate[T](args: Vec[T], dom: T => Interval): Interval
}
object ForwardPropagator {
......@@ -106,7 +106,7 @@ object ForwardPropagator {
.compat(f)
.map(translator =>
new ForwardPropagator {
override def propagate[T](args: IArray[T], dom: T => Interval): Interval =
override def propagate[T](args: Vec[T], dom: T => Interval): Interval =
if(args.map(dom).forall(_.isSingleton)) {
val argValues = translator.argsFromInt(args.map(a => dom(a).lb))
val eval = translator.outToInt(Value(f.compute(argValues)))
......@@ -119,11 +119,11 @@ object ForwardPropagator {
}
trait BackwardPropagator {
def propagate[T](args: IArray[T], out: T, dom: T => Interval): IArray[Interval]
def propagate[T](args: Vec[T], out: T, dom: T => Interval): Vec[Interval]
}
object BackwardPropagator {
case object NoOp extends BackwardPropagator {
override def propagate[T](args: IArray[T], otr: T, dom: T => Interval): IArray[Interval] =
override def propagate[T](args: Vec[T], otr: T, dom: T => Interval): Vec[Interval] =
args.map(dom)
}
}
......@@ -131,39 +131,39 @@ object BackwardPropagator {
abstract class ForwardPropagator1 extends ForwardPropagator {
def propagate(d: Interval): Interval
override def propagate[T](args: IArray[T], dom: T => Interval): Interval =
override def propagate[T](args: Vec[T], dom: T => Interval): Interval =
propagate(dom(args(0)))
}
abstract class ForwardPropagator2 extends ForwardPropagator {
def propagate(l: Interval, r: Interval): Interval
def propagate[T](args: IArray[T], dom: T => Interval): Interval =
def propagate[T](args: Vec[T], dom: T => Interval): Interval =
propagate(dom(args(0)), dom(args(1)))
}
abstract class ForwardPropagatorN extends ForwardPropagator {
def propagate(domains: Seq[Interval]): Interval //TODO: use array
def propagate[T](args: IArray[T], dom: T => Interval): Interval =
def propagate[T](args: Vec[T], dom: T => Interval): Interval =
propagate(args.map(dom).toSeq)
}
abstract class BackwardPropagator1 extends BackwardPropagator {
def propagate(in: Interval, out: Interval): Interval
override def propagate[T](args: IArray[T], out: T, dom: T => Interval): IArray[Interval] =
IArray(propagate(dom(args(0)), dom(out)))
override def propagate[T](args: Vec[T], out: T, dom: T => Interval): Vec[Interval] =
Vec(propagate(dom(args(0)), dom(out)))
}
abstract class BackwardPropagator2 extends BackwardPropagator {
def propagate(in1: Interval, r: Interval, out: Interval): (Interval, Interval)
override def propagate[T](args: IArray[T], out: T, dom: T => Interval): IArray[Interval] = {
override def propagate[T](args: Vec[T], out: T, dom: T => Interval): Vec[Interval] = {
val (d1, d2) = propagate(dom(args(0)), dom(args(1)), dom(out))
IArray(d1, d2)
Vec(d1, d2)
}
}
abstract class BackwardPropagatorN extends BackwardPropagator {
def propagate(in: Array[Interval], out: Interval): Array[Interval]
override def propagate[T](args: IArray[T], out: T, dom: T => Interval): IArray[Interval] = {
IArray.fromArray(propagate(args.map(dom).toArray, dom(out)))
override def propagate[T](args: Vec[T], out: T, dom: T => Interval): Vec[Interval] = {
Vec.fromArray(propagate(args.map(dom).toArray, dom(out)))
}
}
......
package dahu.solvers.problem
import dahu.IArray
import dahu.utils._
import dahu.constraints.domains.{IntDomain, IntervalDomain}
import dahu.constraints._
import dahu.model.types._
......@@ -8,8 +8,6 @@ import dahu.constraints.domains._
import dahu.maps.{ArrayMap, IMapBuilder, SubInt, SubSubInt}
import dahu.model.functions.Fun
import dahu.model.ir._
import dahu.model.problem.IntBoolSatisfactionProblem
import dahu.model.problem.SatisfactionProblemFAST.RootedLazyTree
import dahu.utils.errors._
object IntProblem {
......@@ -17,8 +15,8 @@ object IntProblem {
type Var = Int
type Val = Int
type Vars = IArray[Var]
type Vals = IArray[Val]
type Vars = Vec[Var]
type Vals = Vec[Val]
final case class Func(eval: Vals => Val, fw: ForwardPropagator, bw: BackwardPropagator)
......
package dahu.solvers.constraints
import dahu.IArray
import dahu.utils._
import dahu.constraints.{BackwardPropagator, ForwardPropagator, Propagator}
import dahu.constraints.interval._
import dahu.model.functions._
......@@ -51,26 +51,26 @@ object PropagatorTests extends TestSuite {
}
/** Generator for a seq of domains to be used as the input of the given function.*/
def inputGenerator(f: Fun[_]): Gen[IArray[Interval]] = f match {
def inputGenerator(f: Fun[_]): Gen[Vec[Interval]] = f match {
case x: Fun1[_, _] =>
Gen.sequence[Array[Interval], Interval](Array(gen(x.inType))).map(IArray.fromArray)
Gen.sequence[Array[Interval], Interval](Array(gen(x.inType))).map(Vec.fromArray)
case x: Fun2[_, _, _] =>
Gen
.sequence[Array[Interval], Interval](Array(gen(x.inType1), gen(x.inType2)))
.map(IArray.fromArray)
.map(Vec.fromArray)
case x: FunN[_, _] => // uses an arbitrary number of arguments in [0, 4]
Gen
.choose(0, 4)
.flatMap(n => Gen.sequence[Array[Interval], Interval](Array.fill(n)(gen(x.inTypes))))
.map(IArray.fromArray)
.map(Vec.fromArray)
}
def outputGenerator(f: Fun[_]): Gen[Interval] = gen(f.outType)
def typeTags(f: Fun[_], domains: IArray[Interval]): IArray[TagIsoInt[_]] = f match {
case x: Fun1[_, _] => IArray(x.inType)
case x: Fun2[_, _, _] => IArray(x.inType1, x.inType2)
case x: Fun3[_, _, _, _] =>