Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ghost equality in the generated invariants for range loops over slices #613

Merged
merged 1 commit into from
Feb 6, 2023
Merged
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
22 changes: 9 additions & 13 deletions src/main/scala/viper/gobra/frontend/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ object Desugar {
* var i0 int = 0 // since 'i' can change in the iteration we store the true index in i0
* var j T = c[0] // [v]
* invariant 0 <= i0 && i0 <= len(c)
* invariant i0 < len(c) ==> i0 == i && j == c[i0] // [v] just the j == c[i0] part
* invariant i0 < len(c) ==> i0 == i && j === c[i0] // [v] just the j == c[i0] part
* <invariant...>
* for i0 < length {
* <body>
Expand Down Expand Up @@ -1084,7 +1084,7 @@ object Desugar {
addedInvariantsAfter = (if (hasValue) Vector(
in.Implication(
in.LessCmp(i0, in.Length(c)(src))(src),
in.ExprAssertion(in.EqCmp(j, in.IndexedExp(c, i0, typ)(indexValueSrc))(indexValueSrc))(indexValueSrc))(indexValueSrc))
in.ExprAssertion(in.GhostEqCmp(j, in.IndexedExp(c, i0, typ)(indexValueSrc))(indexValueSrc))(indexValueSrc))(indexValueSrc))
else
Vector())

Expand Down Expand Up @@ -1175,7 +1175,7 @@ object Desugar {
* var i0 int = 0 // since 'i' can change in the iteration we store the true index in i0
* var j T = c[0] // [v]
* invariant 0 <= i0 && i0 <= len(c)
* invariant i0 < len(c) ==> i0 == i && j == c[i0] // [v] just the j == c[i0] part
* invariant i0 < len(c) ==> i0 == i && j === c[i0] // [v] just the j == c[i0] part
* <invariant...>
* for i0 < length {
* <body>
Expand All @@ -1190,9 +1190,9 @@ object Desugar {
def desugarArrSliceAssRange(n: PAssForRange, range: PRange, ass: Vector[PAssignee], spec: PLoopSpec, body: PBlock)(src: Source.Parser.Info): Writer[in.Stmt] = unit(block(for {
exp <- goE(range.exp)

(elemType, typ) = underlyingType(exp.typ) match {
case s: in.SliceT => (s.elems, s)
case a: in.ArrayT => (a.elems, a)
typ = underlyingType(exp.typ) match {
case s: in.SliceT => s
case a: in.ArrayT => a
case _ => violation("Expected slice or array in for-range statement")
}

Expand Down Expand Up @@ -1223,7 +1223,7 @@ object Desugar {
in.ExprAssertion(in.EqCmp(i0, i.op)(src))(src))(src),
in.Implication(
in.LessCmp(i0, in.Length(c)(src))(src),
in.ExprAssertion(in.EqCmp(j.op, in.IndexedExp(c, i0, typ)(indexValueSrc))(indexValueSrc))(indexValueSrc))(indexValueSrc))
in.ExprAssertion(in.GhostEqCmp(j.op, in.IndexedExp(c, i0, typ)(indexValueSrc))(indexValueSrc))(indexValueSrc))(indexValueSrc))
else
Vector(
in.Implication(
Expand Down Expand Up @@ -1344,7 +1344,6 @@ object Desugar {
case in.MapT(k, v, _) => (k.withAddressability(Addressability.exclusiveVariable), v.withAddressability(Addressability.exclusiveVariable))
case _ => violation("unexpected type of range expression")
}
visType = in.SetT(keyType, Addressability.exclusiveVariable)

domain = in.MapKeys(c, underlyingType(exp.typ))(src)

Expand All @@ -1364,7 +1363,6 @@ object Desugar {

(dTerPre, dTer) <- prelude(option(spec.terminationMeasure map terminationMeasureD(ctx, info)))
(dInvPre, dInv) <- prelude(sequence(spec.invariants map assertionD(ctx, info)))
indexValueSrc = meta(range.exp, info).createAnnotatedInfo(Source.NoPermissionToRangeExpressionAnnotation())
addedInvariants = Vector(
in.ExprAssertion(in.AtMostCmp(in.Length(visited.op)(src), in.Length(c)(src))(src))(src),
in.ExprAssertion(in.Subset(visited.op, domain)(src))(src))
Expand Down Expand Up @@ -1420,11 +1418,10 @@ object Desugar {
def desugarMapAssRange(n: PAssForRange, range: PRange, ass: Vector[PAssignee], spec: PLoopSpec, body: PBlock)(src: Source.Parser.Info): Writer[in.Stmt] = unit(block(for {
exp <- goE(range.exp)

(keyType, valType) = underlyingType(exp.typ) match {
case in.MapT(k, v, _) => (k.withAddressability(Addressability.exclusiveVariable), v.withAddressability(Addressability.exclusiveVariable))
keyType = underlyingType(exp.typ) match {
case in.MapT(k, _, _) => k.withAddressability(Addressability.exclusiveVariable)
case _ => violation("unexpected type of range expression")
}
visType = in.SetT(keyType, Addressability.exclusiveVariable)

c <- freshDeclaredExclusiveVar(exp.typ.withAddressability(Addressability.exclusiveVariable), n, info)(src)

Expand All @@ -1446,7 +1443,6 @@ object Desugar {

(dTerPre, dTer) <- prelude(option(spec.terminationMeasure map terminationMeasureD(ctx, info)))
(dInvPre, dInv) <- prelude(sequence(spec.invariants map assertionD(ctx, info)))
indexValueSrc = meta(range.exp, info).createAnnotatedInfo(Source.NoPermissionToRangeExpressionAnnotation())
addedInvariants = Vector(
in.ExprAssertion(in.AtMostCmp(in.Length(visited.op)(src), in.Length(c)(src))(src))(src),
in.ExprAssertion(in.Subset(visited.op, domain)(src))(src))
Expand Down