Skip to content

Commit

Permalink
Merge pull request #289 from ScorexFoundation/i249-rename-col-where-t…
Browse files Browse the repository at this point in the history
…o-filter

Rename collection operation `where` to `filter`
  • Loading branch information
ergomorphic committed Nov 1, 2018
2 parents e77e270 + c7e4174 commit 39f89ed
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 50 deletions.
14 changes: 7 additions & 7 deletions src/main/scala/sigmastate/lang/SigmaBuilder.scala
Expand Up @@ -74,9 +74,9 @@ trait SigmaBuilder {
from: Value[SInt.type],
until: Value[SInt.type]): Value[SCollection[IV]]

def mkWhere[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type]): Value[SCollection[IV]]
def mkFilter[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type]): Value[SCollection[IV]]

def mkExists[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
Expand Down Expand Up @@ -286,10 +286,10 @@ class StdSigmaBuilder extends SigmaBuilder {
until: Value[SInt.type]): Value[SCollection[IV]] =
Slice(input, from, until)

override def mkWhere[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type]): Value[SCollection[IV]] =
Where(input, id, condition)
override def mkFilter[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type]): Value[SCollection[IV]] =
Filter(input, id, condition)

override def mkExists[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
Expand Down
20 changes: 10 additions & 10 deletions src/main/scala/sigmastate/lang/SigmaSpecializer.scala
Expand Up @@ -2,7 +2,7 @@ package sigmastate.lang

import org.bitbucket.inkytonik.kiama.rewriting.Rewriter.{reduce, rewrite, strategy}
import org.ergoplatform.ErgoBox
import sigmastate.SCollection.SByteArray
import sigmastate.SCollection._
import sigmastate.Values.Value.Typed
import sigmastate.Values._
import sigmastate._
Expand Down Expand Up @@ -99,7 +99,7 @@ class SigmaSpecializer(val builder: SigmaBuilder) {
Some(mkUpcast(numValue, tRes))

// Rule: col.size --> SizeOf(col)
case Select(obj, "size", _) =>
case Select(obj, SizeMethodName, _) =>
if (obj.tpe.isCollectionLike)
Some(mkSizeOf(obj.asValue[SCollection[SType]]))
else
Expand Down Expand Up @@ -153,36 +153,36 @@ class SigmaSpecializer(val builder: SigmaBuilder) {
val index = fn.substring(1).toByte
Some(mkSelectField(tuple.asTuple, index))

case Apply(Select(col, "slice", _), Seq(from, until)) =>
case Apply(Select(col, SliceMethodName, _), Seq(from, until)) =>
Some(mkSlice(col.asValue[SCollection[SType]], from.asIntValue, until.asIntValue))

case Apply(Select(col, "where", _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
case Apply(Select(col, FilterMethodName, _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
val tagged = mkTagged(n, t, 21)
val body1 = eval(env + (n -> tagged), body)
Some(mkWhere(col.asValue[SCollection[SType]], tagged.varId, body1.asValue[SBoolean.type]))
Some(mkFilter(col.asValue[SCollection[SType]], tagged.varId, body1.asValue[SBoolean.type]))

case Apply(Select(col,"exists", _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
case Apply(Select(col, ExistsMethodName, _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
val tagged = mkTagged(n, t, 21)
val body1 = eval(env + (n -> tagged), body)
Some(mkExists(col.asValue[SCollection[SType]], tagged.varId, body1.asValue[SBoolean.type]))

case Apply(Select(col,"forall", _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
case Apply(Select(col, ForallMethodName, _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
val tagged = mkTagged(n, t, 21)
val body1 = eval(env + (n -> tagged), body)
Some(mkForAll(col.asValue[SCollection[SType]], tagged.varId, body1.asValue[SBoolean.type]))

case Apply(Select(col,"map", _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
case Apply(Select(col, MapMethodName, _), Seq(Lambda(_, Seq((n, t)), _, Some(body)))) =>
val tagged = mkTagged(n, t, 21)
val body1 = eval(env + (n -> tagged), body)
Some(mkMapCollection(col.asValue[SCollection[SType]], tagged.varId, body1))

case Apply(Select(col,"fold", _), Seq(zero, Lambda(_, Seq((accArg, tAccArg), (opArg, tOpArg)), _, Some(body)))) =>
case Apply(Select(col, FoldMethodName, _), Seq(zero, Lambda(_, Seq((accArg, tAccArg), (opArg, tOpArg)), _, Some(body)))) =>
val taggedAcc = mkTagged(accArg, tAccArg, 21)
val taggedOp = mkTagged(opArg, tOpArg, 22)
val body1 = eval(env ++ Seq(accArg -> taggedAcc, opArg -> taggedOp), body)
Some(mkFold(col.asValue[SCollection[SType]], taggedOp.varId, zero, taggedAcc.varId, body1))

case Apply(Select(col,"getOrElse", _), Seq(index, defaultValue)) =>
case Apply(Select(col, GetOrElseMethodName, _), Seq(index, defaultValue)) =>
val index1 = eval(env, index).asValue[SInt.type]
val defaultValue1 = eval(env, defaultValue).asValue[SType]
Some(mkByIndex(col.asValue[SCollection[SType]], index1, Some(defaultValue1)))
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sigmastate/serialization/OpCodes.scala
Expand Up @@ -103,7 +103,7 @@ object OpCodes extends ValueCodes {
val ByIndexCode : OpCode = (LastConstantCode + 66).toByte
val AppendCode : OpCode = (LastConstantCode + 67).toByte
val SliceCode : OpCode = (LastConstantCode + 68).toByte
val WhereCode : OpCode = (LastConstantCode + 69).toByte
val FilterCode : OpCode = (LastConstantCode + 69).toByte
val TreeLookupCode : OpCode = (LastConstantCode + 70).toByte
val StringConcatCode : OpCode = (LastConstantCode + 71).toByte
val TreeModificationsCode: OpCode = (LastConstantCode + 72).toByte
Expand Down
Expand Up @@ -95,7 +95,7 @@ object ValueSerializer extends SigmaSerializerCompanion[Value[SType]] {
DeserializeContextSerializer(mkDeserializeContext),
DeserializeRegisterSerializer(mkDeserializeRegister),
ExtractRegisterAsSerializer(mkExtractRegisterAs),
WhereSerializer(mkWhere),
FilterSerializer(mkFilter),
SliceSerializer(mkSlice),
AtLeastSerializer(mkAtLeast),
ByIndexSerializer(mkByIndex),
Expand Down
Expand Up @@ -6,14 +6,14 @@ import sigmastate.serialization.OpCodes.OpCode
import sigmastate.serialization.{OpCodes, ValueSerializer}
import sigmastate.utils.Extensions._
import sigmastate.utils.{ByteReader, ByteWriter}
import sigmastate.utxo.Where
import sigmastate.utxo.Filter
import sigmastate.{SBoolean, SCollection, SType}

case class WhereSerializer(cons: (Value[SCollection[SType]], Byte, Value[SBoolean.type]) => Value[SCollection[SType]]) extends ValueSerializer[Where[SType]] {
case class FilterSerializer(cons: (Value[SCollection[SType]], Byte, Value[SBoolean.type]) => Value[SCollection[SType]]) extends ValueSerializer[Filter[SType]] {

override val opCode: OpCode = OpCodes.WhereCode
override val opCode: OpCode = OpCodes.FilterCode

override def serializeBody(obj: Where[SType], w: ByteWriter): Unit =
override def serializeBody(obj: Filter[SType], w: ByteWriter): Unit =
w.put(obj.id)
.putValue(obj.input)
.putValue(obj.condition)
Expand Down
36 changes: 25 additions & 11 deletions src/main/scala/sigmastate/types.scala
Expand Up @@ -462,18 +462,32 @@ object SCollectionType {
object SCollection {
val tIV = STypeIdent("IV")
val tOV = STypeIdent("OV")
val SizeMethodName = "size"
val GetOrElseMethodName = "getOrElse"
val MapMethodName = "map"
val ExistsMethodName = "exists"
val FoldMethodName = "fold"
val ForallMethodName = "forall"
val SliceMethodName = "slice"
val FilterMethodName = "filter"
val methods = Seq(
SMethod("size", SInt),
SMethod("getOrElse", SFunc(IndexedSeq(SCollection(tIV), SInt, tIV), tIV, Seq(STypeParam(tIV)))),
SMethod("map", SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, tOV)),
SCollection(tOV),
Seq(STypeParam(tIV), STypeParam(tOV)))),
SMethod("exists", SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SBoolean, Seq(STypeParam(tIV)))),
SMethod("fold", SFunc(IndexedSeq(SCollection(tIV), tOV, SFunc(IndexedSeq(tOV, tIV), tOV)),
tOV, Seq(STypeParam(tIV), STypeParam(tOV)))),
SMethod("forall", SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SBoolean, Seq(STypeParam(tIV)))),
SMethod("slice", SFunc(IndexedSeq(SCollection(tIV), SInt, SInt), SCollection(tIV), Seq(STypeParam(tIV)))),
SMethod("where", SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SCollection(tIV), Seq(STypeParam(tIV))))
SMethod(SizeMethodName, SInt),
SMethod(GetOrElseMethodName,
SFunc(IndexedSeq(SCollection(tIV), SInt, tIV), tIV, Seq(STypeParam(tIV)))),
SMethod(MapMethodName,
SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, tOV)), SCollection(tOV),
Seq(STypeParam(tIV), STypeParam(tOV)))),
SMethod(ExistsMethodName,
SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SBoolean, Seq(STypeParam(tIV)))),
SMethod(FoldMethodName,
SFunc(IndexedSeq(SCollection(tIV), tOV, SFunc(IndexedSeq(tOV, tIV), tOV)), tOV,
Seq(STypeParam(tIV), STypeParam(tOV)))),
SMethod(ForallMethodName,
SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SBoolean, Seq(STypeParam(tIV)))),
SMethod(SliceMethodName,
SFunc(IndexedSeq(SCollection(tIV), SInt, SInt), SCollection(tIV), Seq(STypeParam(tIV)))),
SMethod(FilterMethodName,
SFunc(IndexedSeq(SCollection(tIV), SFunc(tIV, SBoolean)), SCollection(tIV), Seq(STypeParam(tIV))))
)
def apply[T <: SType](elemType: T): SCollection[T] = SCollectionType(elemType)
def apply[T <: SType](implicit elemType: T, ov: Overload1): SCollection[T] = SCollectionType(elemType)
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/sigmastate/utxo/transformers.scala
Expand Up @@ -110,11 +110,11 @@ case class Slice[IV <: SType](input: Value[SCollection[IV]], from: Value[SInt.ty
input.cost(context) * 2 + from.cost(context) + until.cost(context)
}

case class Where[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type])
case class Filter[IV <: SType](input: Value[SCollection[IV]],
id: Byte,
condition: Value[SBoolean.type])
extends Transformer[SCollection[IV], SCollection[IV]] {
override val opCode: OpCode = OpCodes.WhereCode
override val opCode: OpCode = OpCodes.FilterCode

override def tpe: SCollection[IV] = input.tpe

Expand All @@ -134,7 +134,7 @@ case class Where[IV <: SType](input: Value[SCollection[IV]],
val reduced = intr.eval(localCtx, condition)
reduced match {
case ev: EvaluatedValue[SBoolean.type] => ev.value
case _ => Interpreter.error(s"Expected EvaluatedValue during execution of where but found $reduced")
case _ => Interpreter.error(s"Expected EvaluatedValue during execution of filter but found $reduced")
}
}
ConcreteCollection(filtered)(tpe.elemType)
Expand Down
Expand Up @@ -146,7 +146,7 @@ class TestingInterpreterSpecification extends PropSpec
|}""".stripMargin)
testEval("""{
| val arr = Array(1, 2, 3)
| arr.where {(i: Int) => i < 3} == Array(1, 2)
| arr.filter {(i: Int) => i < 3} == Array(1, 2)
|}""".stripMargin)
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/sigmastate/lang/SigmaParserTest.scala
Expand Up @@ -304,8 +304,8 @@ class SigmaParserTest extends PropSpec with PropertyChecks with Matchers with La
)
parse("OUTPUTS.slice(0, 10)") shouldBe
Apply(Select(Ident("OUTPUTS"), "slice"), Vector(IntConstant(0), IntConstant(10)))
parse("OUTPUTS.where({ (out: Box) => out.value > 0 })") shouldBe
Apply(Select(Ident("OUTPUTS"), "where"),
parse("OUTPUTS.filter({ (out: Box) => out.value > 0 })") shouldBe
Apply(Select(Ident("OUTPUTS"), "filter"),
Vector(Lambda(Vector(("out",SBox)), GT(Select(Ident("out"),"value").asIntValue, 0))))
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/scala/sigmastate/lang/SigmaSpecializerTest.scala
Expand Up @@ -109,8 +109,8 @@ class SigmaSpecializerTest extends PropSpec
22, TrueLeaf, 21, AND(TaggedBoolean(21), GT(TaggedInt(22), IntConstant(1))))
spec("OUTPUTS.slice(0, 10)") shouldBe
Slice(Outputs, IntConstant(0), IntConstant(10))
spec("OUTPUTS.where({ (out: Box) => out.value >= 10 })") shouldBe
Where(Outputs, 21, GE(ExtractAmount(TaggedBox(21)), LongConstant(10)))
spec("OUTPUTS.filter({ (out: Box) => out.value >= 10 })") shouldBe
Filter(Outputs, 21, GE(ExtractAmount(TaggedBox(21)), LongConstant(10)))
}

property("AND flattening predefined") {
Expand Down
2 changes: 1 addition & 1 deletion src/test/scala/sigmastate/lang/SigmaTyperTest.scala
Expand Up @@ -120,7 +120,7 @@ class SigmaTyperTest extends PropSpec with PropertyChecks with Matchers with Lan
typecheck(env, "OUTPUTS.forall({ (out: Box) => out.value >= minToRaise })") shouldBe SBoolean
typecheck(env, "{ val arr = Array(1,2,3); arr.fold(0, { (i1: Int, i2: Int) => i1 + i2 })}") shouldBe SInt
typecheck(env, "OUTPUTS.slice(0, 10)") shouldBe ty("Array[Box]")
typecheck(env, "OUTPUTS.where({ (out: Box) => out.value >= minToRaise })") shouldBe ty("Array[Box]")
typecheck(env, "OUTPUTS.filter({ (out: Box) => out.value >= minToRaise })") shouldBe ty("Array[Box]")
}

property("tuple constructor") {
Expand Down
Expand Up @@ -49,8 +49,8 @@ class TransformersSerializationSpec extends SerializationSpecification {
}
}

property("Where: Serializer round trip") {
forAll { f: Where[SInt.type] =>
property("Filter: Serializer round trip") {
forAll { f: Filter[SInt.type] =>
roundTripTest(f)
}
}
Expand Down
Expand Up @@ -22,7 +22,7 @@ trait TransformerGenerators {
implicit val arbAppend: Arbitrary[Append[SInt.type]] = Arbitrary(appendGen)
implicit val arbSlice: Arbitrary[Slice[SInt.type]] = Arbitrary(sliceGen)
implicit val arbAtLeast: Arbitrary[AtLeast] = Arbitrary(atLeastGen)
implicit val arbWhere: Arbitrary[Where[SInt.type]] = Arbitrary(whereGen)
implicit val arbFilter: Arbitrary[Filter[SInt.type]] = Arbitrary(filterGen)
implicit val sizeOf: Arbitrary[SizeOf[SInt.type]] = Arbitrary(sizeOfGen)
implicit val arbExtractAmount: Arbitrary[ExtractAmount] = Arbitrary(extractAmountGen)
implicit val arbExtractCreationInfo: Arbitrary[ExtractCreationInfo] = Arbitrary(extractCreationInfoGen)
Expand Down Expand Up @@ -73,11 +73,11 @@ trait TransformerGenerators {
input <- arbCCOfBoolConstant.arbitrary
} yield mkAtLeast(bound, input).asInstanceOf[AtLeast]

val whereGen: Gen[Where[SInt.type]] = for {
val filterGen: Gen[Filter[SInt.type]] = for {
col1 <- arbCCOfIntConstant.arbitrary
id <- Arbitrary.arbitrary[Byte]
condition <- booleanConstGen
} yield mkWhere(col1, id, condition).asInstanceOf[Where[SInt.type]]
} yield mkFilter(col1, id, condition).asInstanceOf[Filter[SInt.type]]

val appendGen: Gen[Append[SInt.type]] = for {
col1 <- arbCCOfIntConstant.arbitrary
Expand Down

0 comments on commit 39f89ed

Please sign in to comment.