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

Fix pretty printer for termination measures #831

Merged
merged 2 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
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
30 changes: 22 additions & 8 deletions src/main/scala/viper/silver/ast/pretty/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.language.implicitConversions
import scala.collection.immutable.Queue
import scala.collection.immutable.Queue.{empty => emptyDq}
import viper.silver.ast._
import viper.silver.plugin.standard.termination.DecreasesClause
import viper.silver.verifier.DummyNode

import scala.annotation.tailrec
Expand Down Expand Up @@ -528,13 +529,17 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
case Field(name, typ) =>
text("field") <+> name <> ":" <+> show(typ)
case Method(name, formalArgs, formalReturns, pres, posts, body) =>
// for the time being, the termination plugin transforms decreases clauses into preconditions or postconditions
val (terminationMeasuresInPres, actualPres) = pres.partition(exp => exp.isInstanceOf[DecreasesClause])
val (terminationMeasuresInPosts, actualPosts) = posts.partition(exp => exp.isInstanceOf[DecreasesClause])
group(text("method") <+> name <> nest(defaultIndent, parens(showVars(formalArgs))) <> {
if (formalReturns.isEmpty) nil
else nest(defaultIndent, line <> "returns" <+> parens(showVars(formalReturns)))
}) <>
nest(defaultIndent,
showContracts("requires", pres) <>
showContracts("ensures", posts)
showContracts("requires", actualPres) <>
showContracts("ensures", actualPosts) <>
showDecreasesClauses(terminationMeasuresInPres ++ terminationMeasuresInPosts)
) <>
line <> (
body match {
Expand All @@ -549,11 +554,15 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
case Some(exp) => braces(nest(defaultIndent, line <> show(exp)) <> line)
})
case Function(name, formalArgs, typ, pres, posts, optBody) =>
// for the time being, the termination plugin transforms decreases clauses into preconditions or postconditions
val (terminationMeasuresInPres, actualPres) = pres.partition(exp => exp.isInstanceOf[DecreasesClause])
val (terminationMeasuresInPosts, actualPosts) = posts.partition(exp => exp.isInstanceOf[DecreasesClause])
text("function") <+> name <> nest(defaultIndent, parens(showVars(formalArgs))) <>
":" <+> show(typ) <>
nest(defaultIndent,
showContracts("requires", pres) <>
showContracts("ensures", posts)
showContracts("requires", actualPres) <>
showContracts("ensures", actualPosts) <>
showDecreasesClauses(terminationMeasuresInPres ++ terminationMeasuresInPosts)
) <>
line <>
(optBody match {
Expand All @@ -576,6 +585,11 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
lineIfSomeNonEmpty(contracts) <> ssep(contracts.map(c => text(name) <+> nest(defaultIndent, show(c))), line)
}

/** Shows a list of termination measures. */
def showDecreasesClauses(measures: Seq[Exp]): Cont = {
lineIfSomeNonEmpty(measures) <> ssep(measures.map(c => nest(defaultIndent, show(c))), line)
}

/** Returns `n` lines if at least one element of `s` is non-empty, and an empty document otherwise. */
def lineIfSomeNonEmpty[T](s: Seq[T]*)(implicit n: Int = 1) = {
if (s.forall(e => e != null && e.isEmpty)) nil
Expand Down Expand Up @@ -692,11 +706,11 @@ object FastPrettyPrinter extends FastPrettyPrinterBase with BracketPrettyPrinter
ssep((if (locals == null) Nil else locals map (text("var") <+> showVar(_))) ++ (stmtsToShow map show), line)
}
case While(cond, invs, body) =>
val (terminationMeasures, actualInvs) = invs.partition(exp => exp.isInstanceOf[DecreasesClause])
text("while") <+> parens(show(cond)) <>
nest(defaultIndent,
showContracts("invariant", invs)
) <+> lineIfSomeNonEmpty(invs) <>
showBlock(body)
nest(defaultIndent, showContracts("invariant", actualInvs) <> showDecreasesClauses(terminationMeasures)) <+>
lineIfSomeNonEmpty(invs) <>
showBlock(body)
case If(cond, thn, els) =>
text("if") <+> parens(show(cond)) <+> showBlock(thn) <> showElse(els)
case Label(name, invs) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ case class PredicateInstance(p: String, args: Seq[Exp])(override val pos: Positi
override def verifyExtExp(): VerificationResult = ???

override def prettyPrint: PrettyPrintPrimitives#Cont =
text("@") <> text(p) <> parens(ssep(args map show, char (',') <> space))
text(p) <> parens(ssep(args map show, char (',') <> space))

override lazy val check: Seq[ConsistencyError] = {
args.flatMap(Consistency.checkPure)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package viper.silver.plugin.standard.termination

import viper.silver.ast._
import viper.silver.ast.pretty.FastPrettyPrinter.{ContOps, char, space, ssep, text, toParenDoc}
import viper.silver.ast.pretty.FastPrettyPrinter.{ContOps, char, nil, space, ssep, text, toParenDoc}
import viper.silver.ast.pretty.PrettyPrintPrimitives
import viper.silver.ast.utility.Consistency
import viper.silver.verifier.{ConsistencyError, Failure, VerificationResult}
Expand Down Expand Up @@ -50,8 +50,10 @@ case class DecreasesTuple(tupleExpressions: Seq[Exp] = Nil, override val conditi

override val typ: Type = Bool

override lazy val prettyPrint: PrettyPrintPrimitives#Cont =
text("decreases") <> space <> ssep(tupleExpressions map (toParenDoc(_)), char(',') <> space)
override lazy val prettyPrint: PrettyPrintPrimitives#Cont = {
text("decreases") <>
(if (tupleExpressions.nonEmpty) space <> ssep(tupleExpressions map (toParenDoc(_)), char(',') <> space) else nil)
}

override val extensionSubnodes: Seq[Node] = tupleExpressions ++ condition

Expand Down
Loading