@@ -5,7 +5,6 @@ import cats.syntax.all._
55import cats .{Applicative , Monad }
66import coop .rchain .models .Connective .ConnectiveInstance
77import coop .rchain .models .Connective .ConnectiveInstance ._
8- import coop .rchain .models .Expr .ExprInstance ._
98import coop .rchain .models .Var .VarInstance ._
109import coop .rchain .models ._
1110import coop .rchain .models .rholang .implicits ._
@@ -103,7 +102,7 @@ object Substitute {
103102 implicit def substitutePar [M [_]: Sync ]: Substitute [M , Par ] =
104103 new Substitute [M , Par ] {
105104 def subExp (exprs : Seq [ExprN ])(implicit depth : Int , env : Env [Par ]): M [ParN ] =
106- exprs.toList.reverse.foldM(NilN () : ParN ) { (par, expr) =>
105+ exprs.toList.reverse.foldM(NilN : ParN ) { (par, expr) =>
107106 expr match {
108107 case x : VarN =>
109108 maybeSubstitute[M ](toProtoVar(x)).map {
@@ -117,42 +116,46 @@ object Substitute {
117116 }
118117 }
119118
120- def subConn (conns : Seq [Connective ])(implicit depth : Int , env : Env [Par ]): M [Par ] =
121- conns.toList.reverse.foldM(VectorPar ()) { (par, conn) =>
122- conn.connectiveInstance match {
123- case VarRefBody (v) =>
124- maybeSubstitute[M ](v).map {
125- case Left (_) => par.prepend(conn, depth)
126- case Right (newPar) => newPar ++ par
119+ private def toVarRefBody (x : ConnVarRefN ): VarRefBody = {
120+ val index = x.index
121+ val depth = x.depth
122+ VarRefBody (VarRef (index, depth))
123+ }
124+
125+ def subConn (conns : Seq [ConnectiveN ])(implicit depth : Int , env : Env [Par ]): M [ParN ] =
126+ conns.toList.reverse.foldM(NilN : ParN ) { (par, conn) =>
127+ conn match {
128+ case x : ConnVarRefN =>
129+ maybeSubstitute[M ](toVarRefBody(x).value).map {
130+ case Left (_) => par.combine(conn : ParN )
131+ case Right (newPar) => par.combine(fromProto(newPar))
127132 }
128- case ConnectiveInstance .Empty => par.pure[M ]
129- case ConnAndBody (ConnectiveBody (ps)) =>
130- ps.toVector
131- .traverse(substitutePar[M ].substituteNoSort(_))
132- .map(ps => par.prepend(Connective (ConnAndBody (ConnectiveBody (ps))), depth))
133- case ConnOrBody (ConnectiveBody (ps)) =>
134- ps.toVector
135- .traverse(substitutePar[M ].substituteNoSort(_))
136- .map(ps => par.prepend(Connective (ConnOrBody (ConnectiveBody (ps))), depth))
137- case ConnNotBody (p) =>
133+ case x : ConnAndN =>
134+ x.ps.toVector
135+ .traverse(y => substitutePar[M ].substituteNoSort(toProto(y)).map(fromProto))
136+ .map(ps => par.combine(ConnAndN (ps)))
137+ case x : ConnOrN =>
138+ x.ps.toVector
139+ .traverse(y => substitutePar[M ].substituteNoSort(toProto(y)).map(fromProto))
140+ .map(ps => par.combine(ConnOrN (ps)))
141+ case x : ConnNotN =>
138142 substitutePar[M ]
139- .substituteNoSort(p)
140- .map(p => Connective (ConnNotBody (p)))
141- .map(par.prepend(_, depth))
142- case c : ConnBool => par.prepend(Connective (c), depth).pure[M ]
143- case c : ConnInt => par.prepend(Connective (c), depth).pure[M ]
144- case c : ConnBigInt => par.prepend(Connective (c), depth).pure[M ]
145- case c : ConnString => par.prepend(Connective (c), depth).pure[M ]
146- case c : ConnUri => par.prepend(Connective (c), depth).pure[M ]
147- case c : ConnByteArray => par.prepend(Connective (c), depth).pure[M ]
143+ .substituteNoSort(toProto(x.p))
144+ .map(p => par.combine(ConnNotN (fromProto(p))))
145+ case _ : ConnBoolN .type => par.combine(ConnBoolN ).pure[M ]
146+ case _ : ConnIntN .type => par.combine(ConnIntN ).pure[M ]
147+ case _ : ConnBigIntN .type => par.combine(ConnBigIntN ).pure[M ]
148+ case _ : ConnStringN .type => par.combine(ConnStringN ).pure[M ]
149+ case _ : ConnUriN .type => par.combine(ConnUriN ).pure[M ]
150+ case _ : ConnByteArrayN .type => par.combine(ConnByteArrayN ).pure[M ]
148151 }
149152 }
150153
151154 override def substituteNoSort (term : Par )(implicit depth : Int , env : Env [Par ]): M [Par ] =
152155 for {
153156 _ <- Sync [M ].delay(()) // TODO why removing this breaks StackSafetySpec?
154157 exprs <- subExp(term.exprs.map(fromProtoExpr)).map(toProto)
155- connectives <- subConn(term.connectives)
158+ connectives <- subConn(term.connectives.map(fromProtoConnective)).map(toProto )
156159 sends <- term.sends.toVector.traverse(substituteSend[M ].substituteNoSort(_))
157160 bundles <- term.bundles.toVector.traverse(substituteBundle[M ].substituteNoSort(_))
158161 receives <- term.receives.toVector.traverse(substituteReceive[M ].substituteNoSort(_))
0 commit comments