Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions core/src/main/scala/scalan/Scalan.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import scalan.staged.Transforming

class Scalan
extends Base
with Debugging
// with Debugging
with TypeDescs
with Metadata
with Proxy
with Tuples
with Loops
// with Loops
with TypeSum
with NumericOps
with UnBinOps
Expand All @@ -22,11 +22,11 @@ class Scalan
with UniversalOps
with Functions
with IfThenElse
with PatternMatching
// with PatternMatching
with Transforming
with Analyzing
with Exceptions
with StringOps
// with Analyzing
// with Exceptions
// with StringOps
with RewriteRules
with GraphVizExport
with ViewsModule
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/scala/scalan/TypeDescs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ trait TypeDescs extends Base { self: Scalan =>
def <:<(e: Elem[_]) = tag.tpe <:< e.tag.tpe
def >:>(e: Elem[_]) = e <:< this

if (Base.isDebug) {
debug$ElementCounter(this) += 1
}
// if (Base.isDebug) {
// debug$ElementCounter(this) += 1
// }
}
object Elem {
implicit def rtypeToElem[SA, A](tSA: RType[SA])(implicit lA: Liftables.Liftable[SA,A]): Elem[A] = lA.eW
Expand Down Expand Up @@ -455,7 +455,7 @@ trait TypeDescs extends Base { self: Scalan =>
def invokeUnlifted(e: Elem[_], mc: MethodCall, dataEnv: DataEnv): AnyRef =
e.invokeUnlifted(mc, dataEnv)

private lazy val debug$ElementCounter = counter[Elem[_]]
// private lazy val debug$ElementCounter = counter[Elem[_]]

private[scalan] def getConstructor(clazz: Class[_]) = {
val constructors = clazz.getDeclaredConstructors()
Expand Down
34 changes: 17 additions & 17 deletions core/src/main/scala/scalan/Views.scala
Original file line number Diff line number Diff line change
Expand Up @@ -628,23 +628,23 @@ trait ViewsModule extends impl.ViewsDefs { self: Scalan =>
// ViewArray(parRes, iso)

// Rule: loop(V(start, iso), step, isMatch) ==> iso.to(loop(start, iso.to >> step >> iso.from, iso.to >> isMatch))
case LoopUntil(HasViews(startWithoutViews, iso: Iso[a, b]), step, isMatch) =>
val start1 = startWithoutViews.asRep[a]
implicit val eA = iso.eFrom
implicit val eB = iso.eTo
val step1 = fun { (x: Rep[a]) =>
val x_viewed = iso.to(x)
val res_viewed = step.asRep[b => b](x_viewed) // mirrorApply(step.asRep[b => b], x_viewed)
val res = iso.from(res_viewed)
res
}
val isMatch1 = fun { (x: Rep[a]) =>
val x_viewed = iso.to(x)
val res = isMatch.asRep[b => Boolean](x_viewed) // mirrorApply(isMatch.asRep[b => Boolean], x_viewed)
res
}
val loopRes = LoopUntil(start1, step1, isMatch1)
iso.to(loopRes)
// case LoopUntil(HasViews(startWithoutViews, iso: Iso[a, b]), step, isMatch) =>
// val start1 = startWithoutViews.asRep[a]
// implicit val eA = iso.eFrom
// implicit val eB = iso.eTo
// val step1 = fun { (x: Rep[a]) =>
// val x_viewed = iso.to(x)
// val res_viewed = step.asRep[b => b](x_viewed) // mirrorApply(step.asRep[b => b], x_viewed)
// val res = iso.from(res_viewed)
// res
// }
// val isMatch1 = fun { (x: Rep[a]) =>
// val x_viewed = iso.to(x)
// val res = isMatch.asRep[b => Boolean](x_viewed) // mirrorApply(isMatch.asRep[b => Boolean], x_viewed)
// res
// }
// val loopRes = LoopUntil(start1, step1, isMatch1)
// iso.to(loopRes)

case _ => super.rewriteViews(d)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ trait GraphVizExport { self: Scalan =>
case First(pair) => s"$pair._1"
case Second(pair) => s"$pair._2"
case IfThenElse(c, t, e) => s"if ($c) $t else $e"
case LoopUntil(start, step, isMatch) => s"from $start do $step until $isMatch"
// case LoopUntil(start, step, isMatch) => s"from $start do $step until $isMatch"
case ApplyBinOp(op, lhs, rhs) => s"$lhs ${op.opName} $rhs"
case ApplyUnOp(op, arg) => op match {
case NumericToFloat(_) => s"$arg.toFloat"
Expand Down
24 changes: 17 additions & 7 deletions core/src/main/scala/scalan/primitives/LogicalOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,28 @@ trait LogicalOps extends Base { self: Scalan =>
@inline
private def matchBoolConsts(d: Def[_], lhs: Sym, rhs: Sym, ifTrue: Sym => Sym, ifFalse: Sym => Sym, ifEqual: Sym => Sym, ifNegated: Sym => Sym): Sym =
lhs match {
// op(x, x)
case `rhs` =>
ifEqual(lhs)

// op(!x, x) => ifNegated(!x)
case Def(ApplyUnOp(op, `rhs`)) if op == Not =>
ifNegated(lhs)

// op(true, x) => ifTrue(x) | op(false, x) => ifFalse(x)
case Def(Const(b: Boolean)) =>
if (b) ifTrue(rhs) else ifFalse(rhs)
case _ => rhs match {
case Def(Const(b: Boolean)) =>
if (b) ifTrue(lhs) else ifFalse(lhs)
case Def(ApplyUnOp(op, `lhs`)) if op == Not =>
ifNegated(rhs)
case _ => super.rewriteDef(d)
}

case _ =>
rhs match {
// op(x, true) => ifTrue(x) | op(false, x) => ifFalse(x)
case Def(Const(b: Boolean)) =>
if (b) ifTrue(lhs) else ifFalse(lhs)

// op(x, !x) => ifNegated(!x)
case Def(ApplyUnOp(op, `lhs`)) if op == Not =>
ifNegated(rhs)
case _ => super.rewriteDef(d)
}
}
}
8 changes: 4 additions & 4 deletions core/src/main/scala/scalan/primitives/UniversalOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ trait UniversalOps extends Base { scalan: Scalan =>
def hashCodeRep: Rep[Int] = HashCode[A]().apply(x)
def toStringRep = ToString[A]().apply(x)
}
override def rewriteDef[T](d: Def[T]) = d match {
case ApplyUnOp(ToString(), x) if x.elem == StringElement => x
case _ => super.rewriteDef(d)
}
// override def rewriteDef[T](d: Def[T]) = d match {
// case ApplyUnOp(ToString(), x) if x.elem == StringElement => x
// case _ => super.rewriteDef(d)
// }
}
5 changes: 3 additions & 2 deletions core/src/test/scala/scalan/staged/SlicingTests.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package scalan.staged

import scalan.compilation.{SlicingCompiler, DummyCompiler}
import scalan.{BaseTests, TestContexts, Scalan, Lazy}
import scalan.primitives.StringOps
import scalan.{BaseTests, Lazy, TestContexts, Scalan}

abstract class AbstractSlicingTests extends BaseTests with TestContexts {

class Ctx extends TestContext with Slicing {
class Ctx extends TestContext with Slicing with StringOps {
def createSliceAnalyzer = new SliceAnalyzer

val eInt = element[Int]
Expand Down
24 changes: 24 additions & 0 deletions docs/hard-fork-changes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## A list of hard-fork changes

Please describe here all changes which may lead to hard fork (HF for short).

**Pull requests based on the next HF branch should be rejected,
if they contain HF change, which is not described here**.

### Hard-fork changes in v0.6.0 (since v0.5.0)

1. Removed RW rule `case IsNumericToLong(Def(IsNumericToInt(x))) if x.elem == LongElement => x`
This is a bug, but its fix may lead to hard-fork.
Example:
The follwing expression `MaxLong.toInt.toLong == MaxLong`
with this rule will evaluate to `true`,
without this rule will throw ArithmeticException.
MaxLong here can be any Long which is larger than MaxInt.
With this rule the expression becomes `MaxLong == MaxLong`

2. Removed RW rule `case CM.map(CM.map(_xs, f: RFunc[a, b]), _g: RFunc[_,c]) =>`.
Such kind of transformations in general don't preserve expression eqvivalence
in a strict (Call-By-Value) language.
Having such rule is another bug, which is safe by itself, but cannot
be fixed without HF.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import scalan.Scalan
import scalan.compilation.{CodegenConfig, ScalanCompiler, GraphVizConfig}
import scalan.primitives.Blocks

class KotlinCompiler[+IR <: Scalan with Blocks](val _scalan: IR, val config: CodegenConfig)
class KotlinCompiler[+IR <: ScalanEx](val _scalan: IR, val config: CodegenConfig)
extends ScalanCompiler[IR, KotlinFileCodegen[IR]](_scalan) {
import scalan._

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ package scalan.compilation.kotlin

import java.io.PrintWriter

import scalan.{Scalan, TypeDesc}
import scalan.{TypeDesc, Scalan}
import scalan.compilation.{IndentLevel, FileCodegen, CodegenConfig}
import scalan.meta.ScalanAst._
import scalan.util.PrintExtensions._
import scalan.meta.{SSymName, ScalanAstTransformers}
import scalan.primitives.Blocks
import scalan.primitives.{Blocks, StringOps}

case class GenCtx(module: SUnitDef, writer: PrintWriter)

class KotlinFileCodegen[+IR <: Scalan with Blocks](_scalan: IR, config: CodegenConfig) extends FileCodegen(_scalan, config) {
class ScalanEx extends Scalan with Blocks with StringOps

class KotlinFileCodegen[+IR <: ScalanEx](_scalan: IR, config: CodegenConfig) extends FileCodegen(_scalan, config) {
import scalan._
implicit val context = astContext
val PairType = SSymName("kotlin", "Pair")
Expand Down
39 changes: 39 additions & 0 deletions library-impl/src/main/scala/scalan/RTypeUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package scalan

import spire.syntax.all._
import spire.syntax.trig.trigOps

object RTypeUtil {
import scalan.RType._
import special.collection._

def clone[T](value: T)(implicit t: RType[T]): T = t match {
case prim: PrimitiveType[a] => value
case arrayType: ArrayType[a] =>
val array = value.asInstanceOf[Array[a]]
var copy = Array.ofDim[a](array.length)(arrayType.tA.classTag)
cfor(0)(_ < array.length, _ + 1) { i =>
copy(i) = clone(array(i))(arrayType.tA).asInstanceOf[a]
}
copy.asInstanceOf[T]
case pairType: PairType[a, b] =>
val pair = value.asInstanceOf[Tuple2[a, b]]
return (clone(pair._1)(pairType.tFst), clone(pair._2)(pairType.tSnd)).asInstanceOf[T]
case optionType: OptionType[a] =>
val option = value.asInstanceOf[Option[a]]
val cloned = if (option.isDefined) Some(clone(option.get)(optionType.tA)) else None
cloned.asInstanceOf[T]
case replCollType: ReplCollType[a] =>
val coll = value.asInstanceOf[ReplColl[a]]
val cloned = clone(coll.value)(replCollType.tItem)
(new CReplColl(cloned, coll.length)(replCollType.tItem)).asInstanceOf[T]
case collType: CollType[a] =>
val coll = value.asInstanceOf[Coll[a]]
val cloned = clone(coll.toArray)(ArrayType(collType.tItem))
coll.builder.fromArray(cloned)(collType.tItem).asInstanceOf[T]
case StringType =>
val arr = value.asInstanceOf[String].toArray
arr.mkString.asInstanceOf[T]
case _ => throw new RuntimeException(s"Can't clone ${t}.")
}
}
Loading