diff --git a/model/inline/src/main/scala/aqua/model/inline/Inline.scala b/model/inline/src/main/scala/aqua/model/inline/Inline.scala index baf3b0f2..349174a8 100644 --- a/model/inline/src/main/scala/aqua/model/inline/Inline.scala +++ b/model/inline/src/main/scala/aqua/model/inline/Inline.scala @@ -1,6 +1,6 @@ package aqua.model.inline -import aqua.model.{OpModel, ParModel, SeqModel} +import aqua.model.{EmptyModel, OpModel, ParModel, SeqModel} import aqua.raw.ops.RawTag import aqua.raw.value.ValueRaw import cats.Monoid @@ -33,7 +33,10 @@ private[inline] case class Inline( case l => mergeMode match case SeqMode => - Chain.one(SeqModel.wrap(l: _*)) + val wrapped = SeqModel.wrap(l: _*) + wrapped match + case EmptyModel.leaf => Chain.empty + case _ => Chain.one(wrapped) case ParMode => Chain.one(ParModel.wrap(l: _*)) } diff --git a/model/inline/src/main/scala/aqua/model/inline/RawValueInliner.scala b/model/inline/src/main/scala/aqua/model/inline/RawValueInliner.scala index 8586d866..a7efdd2a 100644 --- a/model/inline/src/main/scala/aqua/model/inline/RawValueInliner.scala +++ b/model/inline/src/main/scala/aqua/model/inline/RawValueInliner.scala @@ -107,7 +107,7 @@ object RawValueInliner extends Logging { ops <- inlineToTree(map) _ = logger.trace("desugarized ops: " + ops) _ = logger.trace("map was: " + map) - } yield vm -> parDesugarPrefix(ops) + } yield vm -> parDesugarPrefix(ops.filterNot(_ == EmptyModel.leaf)) def collectionToModel[S: Mangler: Exports: Arrows]( value: CollectionRaw, diff --git a/model/inline/src/main/scala/aqua/model/inline/TagInliner.scala b/model/inline/src/main/scala/aqua/model/inline/TagInliner.scala index 927b0c51..3634363c 100644 --- a/model/inline/src/main/scala/aqua/model/inline/TagInliner.scala +++ b/model/inline/src/main/scala/aqua/model/inline/TagInliner.scala @@ -35,6 +35,16 @@ object TagInliner extends Logging { private def none[S]: State[S, (Option[OpModel], Option[OpModel.Tree])] = State.pure(None -> None) + private def combineOpsWithSeq(l: Option[OpModel.Tree], r: Option[OpModel.Tree]) = + l match { + case None => r + case Some(a) => + r match { + case None => l + case Some(b) => Some(SeqModel.wrap(a, b)) + } + } + def canonicalizeIfStream[S: Mangler]( vm: ValueModel, ops: Option[OpModel.Tree] @@ -45,7 +55,7 @@ object TagInliner extends Logging { Mangler[S].findAndForbidName(canonName).map { n => val canon = VarModel(n, CanonStreamType(el), l) val canonModel = CanonicalizeModel(vm, CallModel.Export(canon.name, canon.`type`)).leaf - canon -> Some(ops.fold(canonModel)(t => SeqModel.wrap(t, canonModel))) + canon -> combineOpsWithSeq(ops, Option(canonModel)) } case _ => State.pure(vm -> ops) } @@ -69,7 +79,7 @@ object TagInliner extends Logging { flatResult <- flatCanonStream(canonV, Some(canonOp)) } yield { val (resV, resOp) = flatResult - (resV, op.fold(resOp)(t => resOp.map(o => SeqModel.wrap(t, o)))) + (resV, combineOpsWithSeq(op, resOp)) } case v @ VarModel(_, CanonStreamType(_), _) => flatCanonStream(v, op) @@ -88,7 +98,7 @@ object TagInliner extends Logging { val apOp = FlattenModel(canonV, apN).leaf ( apV, - Some(op.fold(apOp)(o => SeqModel.wrap(o, apOp))) + combineOpsWithSeq(op, Option(apOp)) ) } } else { @@ -171,7 +181,7 @@ object TagInliner extends Logging { case (l @ LiteralModel(_, _), p) => for { _ <- Exports[S].resolved(exportTo.name, l) - } yield Some(SeqModel) -> p + } yield None -> p case (v, p) => State.pure(Some(CanonicalizeModel(v, CallModel.callExport(exportTo))) -> p) } @@ -182,7 +192,7 @@ object TagInliner extends Logging { case (l @ LiteralModel(_, _), p) => for { _ <- Exports[S].resolved(assignTo, l) - } yield Some(SeqModel) -> p + } yield None -> p case (v, p) => State.pure(Some(FlattenModel(v, assignTo)) -> p) } @@ -211,7 +221,7 @@ object TagInliner extends Logging { }).flatMap { cd => for { _ <- Exports[S].resolved(assignTo, cd._1) - } yield Some(SeqModel) -> cd._2 + } yield None -> cd._2 } case ClosureTag(arrow, detach) => diff --git a/model/inline/src/test/scala/aqua/model/inline/RawValueInlinerSpec.scala b/model/inline/src/test/scala/aqua/model/inline/RawValueInlinerSpec.scala index 8e1d40f2..da49ede7 100644 --- a/model/inline/src/test/scala/aqua/model/inline/RawValueInlinerSpec.scala +++ b/model/inline/src/test/scala/aqua/model/inline/RawValueInlinerSpec.scala @@ -134,7 +134,7 @@ class RawValueInlinerSpec extends AnyFlatSpec with Matchers { "x", ArrayType(ScalarType.string), Chain.one(IntoIndexModel("y", ScalarType.string)) - ) -> Some(EmptyModel.leaf) + ) -> None ) } diff --git a/model/inline/src/test/scala/aqua/model/inline/TagInlinerSpec.scala b/model/inline/src/test/scala/aqua/model/inline/TagInlinerSpec.scala index eaa12e95..c40402fd 100644 --- a/model/inline/src/test/scala/aqua/model/inline/TagInlinerSpec.scala +++ b/model/inline/src/test/scala/aqua/model/inline/TagInlinerSpec.scala @@ -23,7 +23,7 @@ class TagInlinerSpec extends AnyFlatSpec with Matchers { .value model._1.resolvedExports(canonTo) shouldBe LiteralModel(ValueRaw.Nil.value, ValueRaw.Nil.baseType) - model._2._1 shouldBe Some(SeqModel) + model._2._1 shouldBe None model._2._2 shouldBe None } @@ -42,7 +42,7 @@ class TagInlinerSpec extends AnyFlatSpec with Matchers { ValueRaw.Nil.value, ValueRaw.Nil.baseType ) - model._2._1 shouldBe Some(SeqModel) + model._2._1 shouldBe None model._2._2 shouldBe None } } diff --git a/model/res/src/main/scala/aqua/res/MakeRes.scala b/model/res/src/main/scala/aqua/res/MakeRes.scala index 494b0008..b32d0cb6 100644 --- a/model/res/src/main/scala/aqua/res/MakeRes.scala +++ b/model/res/src/main/scala/aqua/res/MakeRes.scala @@ -57,7 +57,8 @@ object MakeRes { MatchMismatchRes(a, b, s).leaf case ForModel(item, iter, mode) if !isNillLiteral(iter) => FoldRes(item, iter, mode).leaf case RestrictionModel(item, isStream) => RestrictionRes(item, isStream).leaf - case ParModel | DetachModel => ParRes.leaf + case DetachModel => ParRes.leaf + case ParModel => ParRes.leaf case XorModel => XorRes.leaf case NextModel(item) => NextRes(item).leaf case PushToStreamModel(operand @ VarModel(_, StreamType(st), _), exportTo) =>