Skip to content

Commit

Permalink
Add measures in genc tailrec test cases, add empty statement after la…
Browse files Browse the repository at this point in the history
…bel in C code
  • Loading branch information
zhekai-jiang committed Jan 8, 2025
1 parent d0cb790 commit 3b01e4d
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 31 deletions.
4 changes: 3 additions & 1 deletion core/src/main/scala/stainless/genc/CPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,9 @@ class CPrinter(
}

case Labeled(name, block) =>
c"""|$name:
// In C, a label cannot be followed by a variable declaration
// So we add a semicolon to add an empty statement to work around this
c"""|$name: ;
| $block"""

case Lit(lit) => c"$lit"
Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecAliasedArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecAliasedArgs {
def aliased(n: Int, a: Int, b: Int): Int =
require(n >= 0)
decreases(n)
if n == 0 then a
else aliased(n - 1, b, a + b)

Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecComplexArgs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecComplexArgs {
def complexArgs(n: Int): Int =
require(n >= 0)
decreases(n)
if n <= 0 then 1
else complexArgs(n - 1 * 2 + 1) // Complex argument

Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecCountDown.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecNoArguments {
def countDown(n: Int): Int =
require(n >= 0)
decreases(n)
if n == 0 then 0
else countDown(n - 1)

Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecEarlyReturn.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecEarlyReturn {
def earlyReturn(n: Int, acc: Int): Int =
require(n >= 0)
decreases(n)
if n == 0 then acc
else if n == 3 then return acc * 2 // Early return
else earlyReturn(n - 1, acc + 1)
Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecFib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import stainless.io._
object TailRecFib {

def fib(n: Int, i: Int = 0, j: Int = 1): Int =
require(n >= 0)
decreases(n)
if n == 0 then i
else fib(n-1, j, i+j)

Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecFibAliased.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import stainless.io._
object TailRecFibAliased {

def fib(n: Int, i: Int = 0, j: Int = 1): Int =
require(n >= 0)
decreases(n)
if n == 0 then i
else
val res = fib(n-1, j, i+j)
Expand Down
5 changes: 4 additions & 1 deletion frontends/benchmarks/genc/valid/TailRecMultipleCalls.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import stainless.io._

object TailRecMultipleCalls {
def multipleCalls(n: Int, acc: Int): Int =
if n <= 0 then acc
require(n >= 0)
decreases(n)
if n == 0 then acc
else if n == 1 then acc + 2
else if n % 2 == 0 then multipleCalls(n - 1, acc + 1)
else multipleCalls(n - 2, acc + 2)

Expand Down
3 changes: 3 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecNested.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import stainless.io._

object TailRecNested {
def outer(n: Int): Int =
require(n >= 0)
def inner(x: Int): Int =
require(x >= 0)
decreases(x)
if x == 0 then 0
else inner(x - 1)
inner(n)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6
5
20 changes: 15 additions & 5 deletions frontends/benchmarks/genc/valid/TailRecPatternMatching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import stainless.lang._
import stainless.io._

object TailRecPatternMatching {
def patternMatch(x: Option[Int], acc: Int): Int = x match
case None() => acc
case Some(0) => patternMatch(None(), acc + 1)
case Some(n) => patternMatch(Some(n - 1), acc + 1)
def patternMatch(x: Option[Int], acc: Int): Int =
require(x match {
case None() => true
case Some(n) => n >= 1
})
val measure = x match {
case None() => 0
case Some(n) => n
}
decreases(measure)
x match
case None() => acc
case Some(n) if n == 1 => patternMatch(None(), acc + 1)
case Some(n) => patternMatch(Some(n - 1), acc + 1)

@cCode.`export`
def main(): Unit = {
implicit val state = stainless.io.newState
StdOut.println(patternMatch(Some(5), 0)) // Expected: 6
StdOut.println(patternMatch(Some(5), 0)) // Expected: 5
}
}
1 change: 0 additions & 1 deletion frontends/benchmarks/genc/valid/TailRecReturnInLoop.check

This file was deleted.

20 changes: 0 additions & 20 deletions frontends/benchmarks/genc/valid/TailRecReturnInLoop.scala

This file was deleted.

2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecStackOverflow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecStackOverflow {
def even(n: Int): Int =
require(n >= 0)
decreases(n)
if n == 0 then 1
else if n == 1 then 0
else even(n - 2)
Expand Down
2 changes: 2 additions & 0 deletions frontends/benchmarks/genc/valid/TailRecUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import stainless.io._

object TailRecUnit {
def countDown(n: Int): Unit =
require(n >= 0)
decreases(n)
if (n == 0) return
countDown(n - 1)

Expand Down
4 changes: 3 additions & 1 deletion frontends/benchmarks/genc/valid/TailRecUnitIf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import stainless.annotation._
import stainless.lang._
import stainless.io._

object TailRecUnit {
object TailRecUnitIf {
def countDown(n: Int): Unit =
require(n >= 0)
decreases(n)
if (n == 0) return
else countDown(n - 1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import stainless.annotation._
import stainless.lang._
import stainless.io._

object TailRecUnit {
object TailRecUnitWithNoExplicitEnd {
def countDown(n: Int): Unit =
require(n >= 0)
decreases(n)
if (n > 0) countDown(n - 1)

@cCode.`export`
Expand Down

0 comments on commit 3b01e4d

Please sign in to comment.