Skip to content

Commit

Permalink
Add missing termination measures in the sequence and array encodings (#…
Browse files Browse the repository at this point in the history
…758)

* add missing termination measures

* address feedback from pr
  • Loading branch information
jcp19 authored Apr 2, 2024
1 parent 37e386f commit b1bf999
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ class ArrayEncoding extends TypeEncoding with SharedArrayEmbedding {
* function arrayDefault(): ([n]T)°
* ensures len(result) == n
* ensures Forall idx :: {result[idx]} 0 <= idx < n ==> [result[idx] == dflt(T)]
* decreases _
* */
private val exDfltFunc: FunctionGenerator[ComponentParameter] = new FunctionGenerator[ComponentParameter]{
def genFunction(t: ComponentParameter)(ctx: Context): vpr.Function = {
Expand All @@ -362,12 +363,14 @@ class ArrayEncoding extends TypeEncoding with SharedArrayEmbedding {
Seq(vpr.Trigger(Seq(trigger))()),
vpr.Implies(boundaryCondition(vIdx.localVar, t.len)(src), idxEq)()
)()
val terminationMeasure =
synthesized(termination.DecreasesWildcard(None))("This function is assumed to terminate")

vpr.Function(
name = s"${Names.arrayDefaultFunc}_${t.serialize}",
formalArgs = Seq.empty,
typ = vResType,
pres = Seq.empty,
pres = Seq(terminationMeasure),
posts = Vector(lenEq, arrayEq),
body = None
)()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import viper.gobra.translator.util.FunctionGenerator
import viper.gobra.translator.util.ViperUtil.synthesized
import viper.gobra.translator.util.ViperWriter.CodeWriter
import viper.gobra.util.Violation
import viper.silver.plugin.standard.termination
import viper.silver.{ast => vpr}

class SequenceEncoding extends LeafTypeEncoding {
Expand Down Expand Up @@ -225,6 +226,7 @@ class SequenceEncoding extends LeafTypeEncoding {
* requires 0 <= n
* ensures |result| == n
* ensures forall i : Int :: { result[i] } 0 <= i < n ==> result[i] == dfltVal(`T`)
* decreases _
* }}}
*/
private val emptySeqFunc: FunctionGenerator[in.Type] = new FunctionGenerator[in.Type] {
Expand All @@ -245,6 +247,7 @@ class SequenceEncoding extends LeafTypeEncoding {

// preconditions
val pre1 = synthesized(vpr.LeCmp(vpr.IntLit(0)(), nDecl.localVar))("Sequence length might be negative")
val pre2 = synthesized(termination.DecreasesWildcard(None))("This function is assumed to terminate")

// postconditions
val post1 = vpr.EqCmp(vResultLength, nDecl.localVar)()
Expand All @@ -262,7 +265,7 @@ class SequenceEncoding extends LeafTypeEncoding {
name = s"${Names.emptySequenceFunc}_${Names.serializeType(t)}",
formalArgs = Vector(nDecl),
typ = vResultType,
pres = Vector(pre1),
pres = Vector(pre1, pre2),
posts = Vector(post1, post2),
body = None
)()
Expand Down

0 comments on commit b1bf999

Please sign in to comment.