test_ID
stringlengths
3
3
test_file
stringlengths
14
119
ground_truth
stringlengths
70
28.7k
hints_removed
stringlengths
58
28.7k
500
dafny-synthesis_task_id_135.dfy
method NthHexagonalNumber(n: int) returns (hexNum: int) requires n >= 0 ensures hexNum == n * ((2 * n) - 1) { hexNum := n * ((2 * n) - 1); }
method NthHexagonalNumber(n: int) returns (hexNum: int) requires n >= 0 ensures hexNum == n * ((2 * n) - 1) { hexNum := n * ((2 * n) - 1); }
501
dafny-synthesis_task_id_139.dfy
method CircleCircumference(radius: real) returns (circumference: real) requires radius > 0.0 ensures circumference == 2.0 * 3.14159265358979323846 * radius { circumference := 2.0 * 3.14159265358979323846 * radius; }
method CircleCircumference(radius: real) returns (circumference: real) requires radius > 0.0 ensures circumference == 2.0 * 3.14159265358979323846 * radius { circumference := 2.0 * 3.14159265358979323846 * radius; }
502
dafny-synthesis_task_id_14.dfy
method TriangularPrismVolume(base: int, height: int, length: int) returns (volume: int) requires base > 0 requires height > 0 requires length > 0 ensures volume == (base * height * length) / 2 { volume := (base * height * length) / 2; }
method TriangularPrismVolume(base: int, height: int, length: int) returns (volume: int) requires base > 0 requires height > 0 requires length > 0 ensures volume == (base * height * length) / 2 { volume := (base * height * length) / 2; }
503
dafny-synthesis_task_id_142.dfy
method CountIdenticalPositions(a: seq<int>, b: seq<int>, c: seq<int>) returns (count: int) requires |a| == |b| && |b| == |c| ensures count >= 0 ensures count == | set i: int | 0 <= i < |a| && a[i] == b[i] && b[i] == c[i]| { var identical := set i: int | 0 <= i < |a| && a[i] == b[i] && b[i] == c[i]; count := |identical|; }
method CountIdenticalPositions(a: seq<int>, b: seq<int>, c: seq<int>) returns (count: int) requires |a| == |b| && |b| == |c| ensures count >= 0 ensures count == | set i: int | 0 <= i < |a| && a[i] == b[i] && b[i] == c[i]| { var identical := set i: int | 0 <= i < |a| && a[i] == b[i] && b[i] == c[i]; count := |identical|; }
504
dafny-synthesis_task_id_143.dfy
method CountArrays(arrays: seq<array<int>>) returns (count: int) ensures count >= 0 ensures count == |arrays| { count := |arrays|; }
method CountArrays(arrays: seq<array<int>>) returns (count: int) ensures count >= 0 ensures count == |arrays| { count := |arrays|; }
505
dafny-synthesis_task_id_145.dfy
method MaxDifference(a: array<int>) returns (diff: int) requires a.Length > 1 ensures forall i, j :: 0 <= i < a.Length && 0 <= j < a.Length ==> a[i] - a[j] <= diff { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length invariant 1 <= i <= a.Length invariant minVal <= maxVal invariant forall k :: 0 <= k < i ==> minVal <= a[k] && a[k] <= maxVal { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } } diff := maxVal - minVal; }
method MaxDifference(a: array<int>) returns (diff: int) requires a.Length > 1 ensures forall i, j :: 0 <= i < a.Length && 0 <= j < a.Length ==> a[i] - a[j] <= diff { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } } diff := maxVal - minVal; }
506
dafny-synthesis_task_id_161.dfy
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method RemoveElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in a and not in b ensures forall x :: x in result ==> InArray(a, x) && !InArray(b, x) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant forall x :: x in res ==> InArray(a, x) && !InArray(b, x) invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if !InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method RemoveElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in a and not in b ensures forall x :: x in result ==> InArray(a, x) && !InArray(b, x) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length { if !InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
507
dafny-synthesis_task_id_17.dfy
method SquarePerimeter(side: int) returns (perimeter: int) requires side > 0 ensures perimeter == 4 * side { perimeter := 4 * side; }
method SquarePerimeter(side: int) returns (perimeter: int) requires side > 0 ensures perimeter == 4 * side { perimeter := 4 * side; }
508
dafny-synthesis_task_id_170.dfy
function sumTo( a:array<int>, start:int, end:int ) : int requires a != null; requires 0 <= start && start <= end && end <= a.Length; decreases end; reads a; { if (start == end) then 0 else sumTo(a, start, end-1) + a[end-1] } method SumInRange(a: array<int>, start: int, end: int) returns (sum: int) requires a != null requires 0 <= start && start <= end && end <= a.Length ensures sum == sumTo(a, start, end) { sum := 0; for i := start to end invariant start <= i <= end invariant sum == sumTo(a, start, i) { sum := sum + a[i]; } }
function sumTo( a:array<int>, start:int, end:int ) : int requires a != null; requires 0 <= start && start <= end && end <= a.Length; reads a; { if (start == end) then 0 else sumTo(a, start, end-1) + a[end-1] } method SumInRange(a: array<int>, start: int, end: int) returns (sum: int) requires a != null requires 0 <= start && start <= end && end <= a.Length ensures sum == sumTo(a, start, end) { sum := 0; for i := start to end { sum := sum + a[i]; } }
509
dafny-synthesis_task_id_171.dfy
method PentagonPerimeter(side: int) returns (perimeter: int) requires side > 0 ensures perimeter == 5 * side { perimeter := 5 * side; }
method PentagonPerimeter(side: int) returns (perimeter: int) requires side > 0 ensures perimeter == 5 * side { perimeter := 5 * side; }
510
dafny-synthesis_task_id_18.dfy
method RemoveChars(s1: string, s2: string) returns (v: string) ensures |v| <= |s1| ensures forall i :: 0 <= i < |v| ==> (v[i] in s1) && !(v[i] in s2) ensures forall i :: 0 <= i < |s1| ==> (s1[i] in s2) || (s1[i] in v) { var v' : string := []; for i := 0 to |s1| invariant 0 <= i <= |s1| invariant |v'| <= i invariant forall k :: 0 <= k < |v'| ==> (v'[k] in s1) && !(v'[k] in s2) invariant forall k :: 0 <= k < i ==> (s1[k] in s2) || (s1[k] in v') { if !(s1[i] in s2) { v' := v' + [s1[i]]; } } return v'; }
method RemoveChars(s1: string, s2: string) returns (v: string) ensures |v| <= |s1| ensures forall i :: 0 <= i < |v| ==> (v[i] in s1) && !(v[i] in s2) ensures forall i :: 0 <= i < |s1| ==> (s1[i] in s2) || (s1[i] in v) { var v' : string := []; for i := 0 to |s1| { if !(s1[i] in s2) { v' := v' + [s1[i]]; } } return v'; }
511
dafny-synthesis_task_id_2.dfy
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method SharedElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in both a and b ensures forall x :: x in result ==> (InArray(a, x) && InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant forall x :: x in res ==> InArray(a, x) && InArray(b, x) invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method SharedElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in both a and b ensures forall x :: x in result ==> (InArray(a, x) && InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length { if InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
512
dafny-synthesis_task_id_227.dfy
method MinOfThree(a: int, b: int, c: int) returns (min: int) ensures min <= a && min <= b && min <= c ensures (min == a) || (min == b) || (min == c) { if (a <= b && a <= c) { min := a; } else if (b <= a && b <= c) { min := b; } else { min := c; } }
method MinOfThree(a: int, b: int, c: int) returns (min: int) ensures min <= a && min <= b && min <= c ensures (min == a) || (min == b) || (min == c) { if (a <= b && a <= c) { min := a; } else if (b <= a && b <= c) { min := b; } else { min := c; } }
513
dafny-synthesis_task_id_230.dfy
method ReplaceBlanksWithChar(s: string, ch: char) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> (s[i] == ' ' ==> v[i] == ch) && (s[i] != ' ' ==> v[i] == s[i]) { var s' : string := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |s'| == i invariant forall k :: 0 <= k < i ==> (s[k] == ' ' ==> s'[k] == ch) && (s[k] != ' ' ==> s'[k] == s[k]) { if s[i] == ' ' { s' := s' + [ch]; } else { s' := s' + [s[i]]; } } return s'; }
method ReplaceBlanksWithChar(s: string, ch: char) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> (s[i] == ' ' ==> v[i] == ch) && (s[i] != ' ' ==> v[i] == s[i]) { var s' : string := []; for i := 0 to |s| { if s[i] == ' ' { s' := s' + [ch]; } else { s' := s' + [s[i]]; } } return s'; }
514
dafny-synthesis_task_id_233.dfy
method CylinderLateralSurfaceArea(radius: real, height: real) returns (area: real) requires radius > 0.0 && height > 0.0 ensures area == 2.0 * (radius * height) * 3.14 { area := 2.0 * (radius * height) * 3.14; }
method CylinderLateralSurfaceArea(radius: real, height: real) returns (area: real) requires radius > 0.0 && height > 0.0 ensures area == 2.0 * (radius * height) * 3.14 { area := 2.0 * (radius * height) * 3.14; }
515
dafny-synthesis_task_id_234.dfy
method CubeVolume(size: int) returns (volume: int) requires size > 0 ensures volume == size * size * size { volume := size * size * size; }
method CubeVolume(size: int) returns (volume: int) requires size > 0 ensures volume == size * size * size { volume := size * size * size; }
516
dafny-synthesis_task_id_238.dfy
method CountNonEmptySubstrings(s: string) returns (count: int) ensures count >= 0 ensures count == (|s| * (|s| + 1)) / 2 // Formula for the number of non-empty substrings of a string { count := (|s| * (|s| + 1)) / 2; }
method CountNonEmptySubstrings(s: string) returns (count: int) ensures count >= 0 ensures count == (|s| * (|s| + 1)) / 2 // Formula for the number of non-empty substrings of a string { count := (|s| * (|s| + 1)) / 2; }
517
dafny-synthesis_task_id_240.dfy
method ReplaceLastElement(first: seq<int>, second: seq<int>) returns (result: seq<int>) requires |first| > 0 ensures |result| == |first| - 1 + |second| ensures forall i :: 0 <= i < |first| - 1 ==> result[i] == first[i] ensures forall i :: |first| - 1 <= i < |result| ==> result[i] == second[i - |first| + 1] { result := first[0..|first| - 1] + second; }
method ReplaceLastElement(first: seq<int>, second: seq<int>) returns (result: seq<int>) requires |first| > 0 ensures |result| == |first| - 1 + |second| ensures forall i :: 0 <= i < |first| - 1 ==> result[i] == first[i] ensures forall i :: |first| - 1 <= i < |result| ==> result[i] == second[i - |first| + 1] { result := first[0..|first| - 1] + second; }
518
dafny-synthesis_task_id_242.dfy
method CountCharacters(s: string) returns (count: int) ensures count >= 0 ensures count == |s| { count := |s|; }
method CountCharacters(s: string) returns (count: int) ensures count >= 0 ensures count == |s| { count := |s|; }
519
dafny-synthesis_task_id_249.dfy
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method Intersection(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in both a and b ensures forall x :: x in result ==> (InArray(a, x) && InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant forall x :: x in res ==> (InArray(a, x) && InArray(b, x)) invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method Intersection(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are in both a and b ensures forall x :: x in result ==> (InArray(a, x) && InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length { if InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } result := res; }
520
dafny-synthesis_task_id_251.dfy
method InsertBeforeEach(s: seq<string>, x: string) returns (v: seq<string>) ensures |v| == 2 * |s| ensures forall i :: 0 <= i < |s| ==> v[2*i] == x && v[2*i + 1] == s[i] { v := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |v| == 2 * i invariant forall j :: 0 <= j < i ==> v[2*j] == x && v[2*j + 1] == s[j] { v := v + [x, s[i]]; } }
method InsertBeforeEach(s: seq<string>, x: string) returns (v: seq<string>) ensures |v| == 2 * |s| ensures forall i :: 0 <= i < |s| ==> v[2*i] == x && v[2*i + 1] == s[i] { v := []; for i := 0 to |s| { v := v + [x, s[i]]; } }
521
dafny-synthesis_task_id_257.dfy
method Swap(a: int, b: int) returns (result: seq<int>) ensures |result| == 2 ensures result[0] == b ensures result[1] == a { result := [b, a]; }
method Swap(a: int, b: int) returns (result: seq<int>) ensures |result| == 2 ensures result[0] == b ensures result[1] == a { result := [b, a]; }
522
dafny-synthesis_task_id_261.dfy
method ElementWiseDivision(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| requires forall i :: 0 <= i < |b| ==> b[i] != 0 ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] / b[i] { result := []; var i := 0; while i < |a| invariant 0 <= i <= |a| invariant |result| == i invariant forall k :: 0 <= k < i ==> result[k] == a[k] / b[k] { result := result + [a[i] / b[i]]; i := i + 1; } }
method ElementWiseDivision(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| requires forall i :: 0 <= i < |b| ==> b[i] != 0 ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] / b[i] { result := []; var i := 0; while i < |a| { result := result + [a[i] / b[i]]; i := i + 1; } }
523
dafny-synthesis_task_id_262.dfy
method SplitArray(arr: array<int>, L: int) returns (firstPart: seq<int>, secondPart: seq<int>) requires 0 <= L <= arr.Length ensures |firstPart| == L ensures |secondPart| == arr.Length - L ensures firstPart + secondPart == arr[..] { firstPart := arr[..L]; secondPart := arr[L..]; }
method SplitArray(arr: array<int>, L: int) returns (firstPart: seq<int>, secondPart: seq<int>) requires 0 <= L <= arr.Length ensures |firstPart| == L ensures |secondPart| == arr.Length - L ensures firstPart + secondPart == arr[..] { firstPart := arr[..L]; secondPart := arr[L..]; }
524
dafny-synthesis_task_id_264.dfy
method DogYears(humanYears: int) returns (dogYears: int) requires humanYears >= 0 ensures dogYears == 7 * humanYears { dogYears := 7 * humanYears; }
method DogYears(humanYears: int) returns (dogYears: int) requires humanYears >= 0 ensures dogYears == 7 * humanYears { dogYears := 7 * humanYears; }
525
dafny-synthesis_task_id_266.dfy
method LateralSurfaceArea(size: int) returns (area: int) requires size > 0 ensures area == 4 * size * size { area := 4 * size * size; }
method LateralSurfaceArea(size: int) returns (area: int) requires size > 0 ensures area == 4 * size * size { area := 4 * size * size; }
526
dafny-synthesis_task_id_267.dfy
method SumOfSquaresOfFirstNOddNumbers(n: int) returns (sum: int) requires n >= 0 ensures sum == (n * (2 * n - 1) * (2 * n + 1)) / 3 { sum := 0; var i := 1; for k:=0 to n invariant 0 <= k <= n invariant sum == k * (2 * k - 1) * (2 * k + 1) / 3 invariant i == 2 * k + 1 { sum := sum + i * i; i := i + 2; } }
method SumOfSquaresOfFirstNOddNumbers(n: int) returns (sum: int) requires n >= 0 ensures sum == (n * (2 * n - 1) * (2 * n + 1)) / 3 { sum := 0; var i := 1; for k:=0 to n { sum := sum + i * i; i := i + 2; } }
527
dafny-synthesis_task_id_268.dfy
method StarNumber(n: int) returns (star: int) requires n >= 0 ensures star == 6 * n * (n - 1) + 1 { star := 6 * n * (n - 1) + 1; }
method StarNumber(n: int) returns (star: int) requires n >= 0 ensures star == 6 * n * (n - 1) + 1 { star := 6 * n * (n - 1) + 1; }
528
dafny-synthesis_task_id_269.dfy
method AsciiValue(c: char) returns (ascii: int) ensures ascii == c as int { ascii := c as int; }
method AsciiValue(c: char) returns (ascii: int) ensures ascii == c as int { ascii := c as int; }
529
dafny-synthesis_task_id_273.dfy
method SubtractSequences(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] - b[i] { result := []; var i := 0; while i < |a| invariant 0 <= i <= |a| invariant |result| == i invariant forall k :: 0 <= k < i ==> result[k] == a[k] - b[k] { result := result + [a[i] - b[i]]; i := i + 1; } }
method SubtractSequences(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] - b[i] { result := []; var i := 0; while i < |a| { result := result + [a[i] - b[i]]; i := i + 1; } }
530
dafny-synthesis_task_id_276.dfy
method CylinderVolume(radius: real, height: real) returns (volume: real) requires radius > 0.0 requires height > 0.0 ensures volume == 3.14159265359 * radius * radius * height { volume := 3.14159265359 * radius * radius * height; }
method CylinderVolume(radius: real, height: real) returns (volume: real) requires radius > 0.0 requires height > 0.0 ensures volume == 3.14159265359 * radius * radius * height { volume := 3.14159265359 * radius * radius * height; }
531
dafny-synthesis_task_id_279.dfy
method NthDecagonalNumber(n: int) returns (decagonal: int) requires n >= 0 ensures decagonal == 4 * n * n - 3 * n { decagonal := 4 * n * n - 3 * n; }
method NthDecagonalNumber(n: int) returns (decagonal: int) requires n >= 0 ensures decagonal == 4 * n * n - 3 * n { decagonal := 4 * n * n - 3 * n; }
532
dafny-synthesis_task_id_282.dfy
method ElementWiseSubtraction(a: array<int>, b: array<int>) returns (result: array<int>) requires a != null && b != null requires a.Length == b.Length ensures result != null ensures result.Length == a.Length ensures forall i :: 0 <= i < result.Length ==> result[i] == a[i] - b[i] { result := new int[a.Length]; var i := 0; while i < a.Length invariant 0 <= i <= a.Length invariant result.Length == a.Length invariant forall k :: 0 <= k < i ==> result[k] == a[k] - b[k] { result[i] := a[i] - b[i]; i := i + 1; } }
method ElementWiseSubtraction(a: array<int>, b: array<int>) returns (result: array<int>) requires a != null && b != null requires a.Length == b.Length ensures result != null ensures result.Length == a.Length ensures forall i :: 0 <= i < result.Length ==> result[i] == a[i] - b[i] { result := new int[a.Length]; var i := 0; while i < a.Length { result[i] := a[i] - b[i]; i := i + 1; } }
533
dafny-synthesis_task_id_284.dfy
method AllElementsEqual(a: array<int>, n: int) returns (result: bool) requires a != null ensures result ==> forall i :: 0 <= i < a.Length ==> a[i] == n ensures !result ==> exists i :: 0 <= i < a.Length && a[i] != n { result := true; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant result ==> forall k :: 0 <= k < i ==> a[k] == n { if a[i] != n { result := false; break; } } }
method AllElementsEqual(a: array<int>, n: int) returns (result: bool) requires a != null ensures result ==> forall i :: 0 <= i < a.Length ==> a[i] == n ensures !result ==> exists i :: 0 <= i < a.Length && a[i] != n { result := true; for i := 0 to a.Length { if a[i] != n { result := false; break; } } }
534
dafny-synthesis_task_id_290.dfy
method MaxLengthList(lists: seq<seq<int>>) returns (maxList: seq<int>) requires |lists| > 0 ensures forall l :: l in lists ==> |l| <= |maxList| ensures maxList in lists { maxList := lists[0]; for i := 1 to |lists| invariant 1 <= i <= |lists| invariant forall l :: l in lists[..i] ==> |l| <= |maxList| invariant maxList in lists[..i] { if |lists[i]| > |maxList| { maxList := lists[i]; } } }
method MaxLengthList(lists: seq<seq<int>>) returns (maxList: seq<int>) requires |lists| > 0 ensures forall l :: l in lists ==> |l| <= |maxList| ensures maxList in lists { maxList := lists[0]; for i := 1 to |lists| { if |lists[i]| > |maxList| { maxList := lists[i]; } } }
535
dafny-synthesis_task_id_292.dfy
method Quotient(a: int, b: int) returns (result: int) requires b != 0 ensures result == a / b { result := a / b; }
method Quotient(a: int, b: int) returns (result: int) requires b != 0 ensures result == a / b { result := a / b; }
536
dafny-synthesis_task_id_3.dfy
method IsNonPrime(n: int) returns (result: bool) requires n >= 2 ensures result <==> (exists k :: 2 <= k < n && n % k == 0) { result := false; var i := 2; while i <= n/2 invariant 2 <= i invariant result <==> (exists k :: 2 <= k < i && n % k == 0) { if n % i == 0 { result := true; break; } i := i + 1; } }
method IsNonPrime(n: int) returns (result: bool) requires n >= 2 ensures result <==> (exists k :: 2 <= k < n && n % k == 0) { result := false; var i := 2; while i <= n/2 { if n % i == 0 { result := true; break; } i := i + 1; } }
537
dafny-synthesis_task_id_304.dfy
method ElementAtIndexAfterRotation(l: seq<int>, n: int, index: int) returns (element: int) requires n >= 0 requires 0 <= index < |l| ensures element == l[(index - n + |l|) % |l|] { element := l[(index - n + |l|) % |l|]; }
method ElementAtIndexAfterRotation(l: seq<int>, n: int, index: int) returns (element: int) requires n >= 0 requires 0 <= index < |l| ensures element == l[(index - n + |l|) % |l|] { element := l[(index - n + |l|) % |l|]; }
538
dafny-synthesis_task_id_307.dfy
method DeepCopySeq(s: seq<int>) returns (copy: seq<int>) ensures |copy| == |s| ensures forall i :: 0 <= i < |s| ==> copy[i] == s[i] { var newSeq: seq<int> := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |newSeq| == i invariant forall k :: 0 <= k < i ==> newSeq[k] == s[k] { newSeq := newSeq + [s[i]]; } return newSeq; }
method DeepCopySeq(s: seq<int>) returns (copy: seq<int>) ensures |copy| == |s| ensures forall i :: 0 <= i < |s| ==> copy[i] == s[i] { var newSeq: seq<int> := []; for i := 0 to |s| { newSeq := newSeq + [s[i]]; } return newSeq; }
539
dafny-synthesis_task_id_309.dfy
method Max(a: int, b: int) returns (maxValue: int) ensures maxValue == a || maxValue == b ensures maxValue >= a && maxValue >= b { if a >= b { maxValue := a; } else { maxValue := b; } }
method Max(a: int, b: int) returns (maxValue: int) ensures maxValue == a || maxValue == b ensures maxValue >= a && maxValue >= b { if a >= b { maxValue := a; } else { maxValue := b; } }
540
dafny-synthesis_task_id_310.dfy
method ToCharArray(s: string) returns (a: array<char>) ensures a.Length == |s| ensures forall i :: 0 <= i < |s| ==> a[i] == s[i] { a := new char[|s|]; for i := 0 to |s| invariant 0 <= i <= |s| invariant a.Length == |s| invariant forall k :: 0 <= k < i ==> a[k] == s[k] { a[i] := s[i]; } }
method ToCharArray(s: string) returns (a: array<char>) ensures a.Length == |s| ensures forall i :: 0 <= i < |s| ==> a[i] == s[i] { a := new char[|s|]; for i := 0 to |s| { a[i] := s[i]; } }
541
dafny-synthesis_task_id_312.dfy
method ConeVolume(radius: real, height: real) returns (volume: real) requires radius > 0.0 && height > 0.0 ensures volume == (1.0/3.0) * (3.14159265358979323846) * radius * radius * height { volume := (1.0/3.0) * (3.14159265358979323846) * radius * radius * height; }
method ConeVolume(radius: real, height: real) returns (volume: real) requires radius > 0.0 && height > 0.0 ensures volume == (1.0/3.0) * (3.14159265358979323846) * radius * radius * height { volume := (1.0/3.0) * (3.14159265358979323846) * radius * radius * height; }
542
dafny-synthesis_task_id_396.dfy
method StartAndEndWithSameChar(s: string) returns (result: bool) requires |s| > 0 ensures result <==> s[0] == s[|s| - 1] { result := s[0] == s[|s| - 1]; }
method StartAndEndWithSameChar(s: string) returns (result: bool) requires |s| > 0 ensures result <==> s[0] == s[|s| - 1] { result := s[0] == s[|s| - 1]; }
543
dafny-synthesis_task_id_397.dfy
method MedianOfThree(a: int, b: int, c: int) returns (median: int) ensures median == a || median == b || median == c ensures (median >= a && median <= b) || (median >= b && median <= a) || (median >= a && median <= c) || (median >= c && median <= a) || (median >= b && median <= c) || (median >= c && median <= b) { if ((a <= b && b <= c) || (c <= b && b <= a)) { median := b; } else if ((b <= a && a <= c) || (c <= a && a <= b)) { median := a; } else { median := c; } }
method MedianOfThree(a: int, b: int, c: int) returns (median: int) ensures median == a || median == b || median == c ensures (median >= a && median <= b) || (median >= b && median <= a) || (median >= a && median <= c) || (median >= c && median <= a) || (median >= b && median <= c) || (median >= c && median <= b) { if ((a <= b && b <= c) || (c <= b && b <= a)) { median := b; } else if ((b <= a && a <= c) || (c <= a && a <= b)) { median := a; } else { median := c; } }
544
dafny-synthesis_task_id_399.dfy
method BitwiseXOR(a: seq<bv32>, b: seq<bv32>) returns (result: seq<bv32>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] ^ b[i] { result := []; var i := 0; while i < |a| invariant 0 <= i <= |a| invariant |result| == i invariant forall k :: 0 <= k < i ==> result[k] == a[k] ^ b[k] { result := result + [a[i] ^ b[i]]; i := i + 1; } }
method BitwiseXOR(a: seq<bv32>, b: seq<bv32>) returns (result: seq<bv32>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] ^ b[i] { result := []; var i := 0; while i < |a| { result := result + [a[i] ^ b[i]]; i := i + 1; } }
545
dafny-synthesis_task_id_401.dfy
method IndexWiseAddition(a: seq<seq<int>>, b: seq<seq<int>>) returns (result: seq<seq<int>>) requires |a| > 0 && |b| > 0 requires |a| == |b| requires forall i :: 0 <= i < |a| ==> |a[i]| == |b[i]| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> |result[i]| == |a[i]| ensures forall i :: 0 <= i < |result| ==> forall j :: 0 <= j < |result[i]| ==> result[i][j] == a[i][j] + b[i][j] { result := []; for i := 0 to |a| invariant 0 <= i <= |a| invariant |result| == i invariant forall k :: 0 <= k < i ==> |result[k]| == |a[k]| invariant forall k :: 0 <= k < i ==> forall j :: 0 <= j < |result[k]| ==> result[k][j] == a[k][j] + b[k][j] { var subResult := []; for j := 0 to |a[i]| invariant 0 <= j <= |a[i]| invariant |subResult| == j invariant forall k :: 0 <= k < j ==> subResult[k] == a[i][k] + b[i][k] { subResult := subResult + [a[i][j] + b[i][j]]; } result := result + [subResult]; } }
method IndexWiseAddition(a: seq<seq<int>>, b: seq<seq<int>>) returns (result: seq<seq<int>>) requires |a| > 0 && |b| > 0 requires |a| == |b| requires forall i :: 0 <= i < |a| ==> |a[i]| == |b[i]| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> |result[i]| == |a[i]| ensures forall i :: 0 <= i < |result| ==> forall j :: 0 <= j < |result[i]| ==> result[i][j] == a[i][j] + b[i][j] { result := []; for i := 0 to |a| { var subResult := []; for j := 0 to |a[i]| { subResult := subResult + [a[i][j] + b[i][j]]; } result := result + [subResult]; } }
546
dafny-synthesis_task_id_404.dfy
method Min(a: int, b: int) returns (minValue: int) ensures minValue == a || minValue == b ensures minValue <= a && minValue <= b { if a <= b { minValue := a; } else { minValue := b; } }
method Min(a: int, b: int) returns (minValue: int) ensures minValue == a || minValue == b ensures minValue <= a && minValue <= b { if a <= b { minValue := a; } else { minValue := b; } }
547
dafny-synthesis_task_id_406.dfy
method IsOdd(n: int) returns (result: bool) ensures result <==> n % 2 == 1 { result := n % 2 == 1; }
method IsOdd(n: int) returns (result: bool) ensures result <==> n % 2 == 1 { result := n % 2 == 1; }
548
dafny-synthesis_task_id_412.dfy
/** * Remove odd numbers from an array of numbers **/ predicate IsEven(n: int) { n % 2 == 0 } method RemoveOddNumbers(arr: array<int>) returns (evenList: seq<int>) // All numbers in the output are even and exist in the input ensures forall i :: 0 <= i < |evenList| ==> IsEven(evenList[i]) && evenList[i] in arr[..] // All even numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsEven(arr[i]) ==> arr[i] in evenList { evenList := []; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant 0 <= |evenList| <= i invariant forall k :: 0 <= k < |evenList| ==> IsEven(evenList[k]) && evenList[k] in arr[..] invariant forall k :: 0 <= k < i && IsEven(arr[k]) ==> arr[k] in evenList { if IsEven(arr[i]) { evenList := evenList + [arr[i]]; } } }
/** * Remove odd numbers from an array of numbers **/ predicate IsEven(n: int) { n % 2 == 0 } method RemoveOddNumbers(arr: array<int>) returns (evenList: seq<int>) // All numbers in the output are even and exist in the input ensures forall i :: 0 <= i < |evenList| ==> IsEven(evenList[i]) && evenList[i] in arr[..] // All even numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsEven(arr[i]) ==> arr[i] in evenList { evenList := []; for i := 0 to arr.Length { if IsEven(arr[i]) { evenList := evenList + [arr[i]]; } } }
549
dafny-synthesis_task_id_414.dfy
method AnyValueExists(seq1: seq<int>, seq2: seq<int>) returns (result: bool) ensures result <==> (exists i :: 0 <= i < |seq1| && seq1[i] in seq2) { result := false; for i := 0 to |seq1| invariant 0 <= i <= |seq1| invariant result <==> (exists k :: 0 <= k < i && seq1[k] in seq2) { if seq1[i] in seq2 { result := true; break; } } }
method AnyValueExists(seq1: seq<int>, seq2: seq<int>) returns (result: bool) ensures result <==> (exists i :: 0 <= i < |seq1| && seq1[i] in seq2) { result := false; for i := 0 to |seq1| { if seq1[i] in seq2 { result := true; break; } } }
550
dafny-synthesis_task_id_424.dfy
method ExtractRearChars(l: seq<string>) returns (r: seq<char>) requires forall i :: 0 <= i < |l| ==> |l[i]| > 0 ensures |r| == |l| ensures forall i :: 0 <= i < |l| ==> r[i] == l[i][|l[i]| - 1] { var rearChars: seq<char> := []; for i := 0 to |l| invariant 0 <= i <= |l| invariant |rearChars| == i invariant forall k :: 0 <= k < i ==> rearChars[k] == l[k][|l[k]| - 1] { rearChars := rearChars + [l[i][|l[i]| - 1]]; } return rearChars; }
method ExtractRearChars(l: seq<string>) returns (r: seq<char>) requires forall i :: 0 <= i < |l| ==> |l[i]| > 0 ensures |r| == |l| ensures forall i :: 0 <= i < |l| ==> r[i] == l[i][|l[i]| - 1] { var rearChars: seq<char> := []; for i := 0 to |l| { rearChars := rearChars + [l[i][|l[i]| - 1]]; } return rearChars; }
551
dafny-synthesis_task_id_426.dfy
/** * Filter odd numbers from an array of numbers **/ predicate IsOdd(n: int) { n % 2 != 0 } method FilterOddNumbers(arr: array<int>) returns (oddList: seq<int>) // All numbers in the output are odd and exist in the input ensures forall i :: 0 <= i < |oddList| ==> IsOdd(oddList[i]) && oddList[i] in arr[..] // All odd numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsOdd(arr[i]) ==> arr[i] in oddList { oddList := []; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant 0 <= |oddList| <= i invariant forall k :: 0 <= k < |oddList| ==> IsOdd(oddList[k]) && oddList[k] in arr[..] invariant forall k :: 0 <= k < i && IsOdd(arr[k]) ==> arr[k] in oddList { if IsOdd(arr[i]) { oddList := oddList + [arr[i]]; } } }
/** * Filter odd numbers from an array of numbers **/ predicate IsOdd(n: int) { n % 2 != 0 } method FilterOddNumbers(arr: array<int>) returns (oddList: seq<int>) // All numbers in the output are odd and exist in the input ensures forall i :: 0 <= i < |oddList| ==> IsOdd(oddList[i]) && oddList[i] in arr[..] // All odd numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsOdd(arr[i]) ==> arr[i] in oddList { oddList := []; for i := 0 to arr.Length { if IsOdd(arr[i]) { oddList := oddList + [arr[i]]; } } }
552
dafny-synthesis_task_id_430.dfy
method ParabolaDirectrix(a: real, h: real, k: real) returns (directrix: real) requires a != 0.0 ensures directrix == k - 1.0 / (4.0 * a) { directrix := k - 1.0 / (4.0 * a); }
method ParabolaDirectrix(a: real, h: real, k: real) returns (directrix: real) requires a != 0.0 ensures directrix == k - 1.0 / (4.0 * a) { directrix := k - 1.0 / (4.0 * a); }
553
dafny-synthesis_task_id_431.dfy
method HasCommonElement(a: array<int>, b: array<int>) returns (result: bool) requires a != null && b != null ensures result ==> exists i, j :: 0 <= i < a.Length && 0 <= j < b.Length && a[i] == b[j] ensures !result ==> forall i, j :: 0 <= i < a.Length && 0 <= j < b.Length ==> a[i] != b[j] { result := false; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant !result ==> forall k, j :: 0 <= k < i && 0 <= j < b.Length ==> a[k] != b[j] { for j := 0 to b.Length invariant 0 <= j <= b.Length invariant !result ==> forall k :: 0 <= k < j ==> a[i] != b[k] { if a[i] == b[j] { result := true; return; } } } }
method HasCommonElement(a: array<int>, b: array<int>) returns (result: bool) requires a != null && b != null ensures result ==> exists i, j :: 0 <= i < a.Length && 0 <= j < b.Length && a[i] == b[j] ensures !result ==> forall i, j :: 0 <= i < a.Length && 0 <= j < b.Length ==> a[i] != b[j] { result := false; for i := 0 to a.Length { for j := 0 to b.Length { if a[i] == b[j] { result := true; return; } } } }
554
dafny-synthesis_task_id_432.dfy
method MedianLength(a: int, b: int) returns (median: int) requires a > 0 && b > 0 ensures median == (a + b) / 2 { median := (a + b) / 2; }
method MedianLength(a: int, b: int) returns (median: int) requires a > 0 && b > 0 ensures median == (a + b) / 2 { median := (a + b) / 2; }
555
dafny-synthesis_task_id_433.dfy
method IsGreater(n: int, a: array<int>) returns (result: bool) requires a != null ensures result ==> forall i :: 0 <= i < a.Length ==> n > a[i] ensures !result ==> exists i :: 0 <= i < a.Length && n <= a[i] { result := true; var i := 0; while i < a.Length invariant 0 <= i <= a.Length invariant result ==> forall k :: 0 <= k < i ==> n > a[k] invariant !result ==> exists k :: 0 <= k < i && n <= a[k] { if n <= a[i] { result := false; break; } i := i + 1; } }
method IsGreater(n: int, a: array<int>) returns (result: bool) requires a != null ensures result ==> forall i :: 0 <= i < a.Length ==> n > a[i] ensures !result ==> exists i :: 0 <= i < a.Length && n <= a[i] { result := true; var i := 0; while i < a.Length { if n <= a[i] { result := false; break; } i := i + 1; } }
556
dafny-synthesis_task_id_435.dfy
method LastDigit(n: int) returns (d: int) requires n >= 0 ensures 0 <= d < 10 ensures n % 10 == d { d := n % 10; }
method LastDigit(n: int) returns (d: int) requires n >= 0 ensures 0 <= d < 10 ensures n % 10 == d { d := n % 10; }
557
dafny-synthesis_task_id_436.dfy
/** * Find negative numbers from an array of numbers **/ predicate IsNegative(n: int) { n < 0 } method FindNegativeNumbers(arr: array<int>) returns (negativeList: seq<int>) // All numbers in the output are negative and exist in the input ensures forall i :: 0 <= i < |negativeList| ==> IsNegative(negativeList[i]) && negativeList[i] in arr[..] // All negative numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsNegative(arr[i]) ==> arr[i] in negativeList { negativeList := []; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant 0 <= |negativeList| <= i invariant forall k :: 0 <= k < |negativeList| ==> IsNegative(negativeList[k]) && negativeList[k] in arr[..] invariant forall k :: 0 <= k < i && IsNegative(arr[k]) ==> arr[k] in negativeList { if IsNegative(arr[i]) { negativeList := negativeList + [arr[i]]; } } }
/** * Find negative numbers from an array of numbers **/ predicate IsNegative(n: int) { n < 0 } method FindNegativeNumbers(arr: array<int>) returns (negativeList: seq<int>) // All numbers in the output are negative and exist in the input ensures forall i :: 0 <= i < |negativeList| ==> IsNegative(negativeList[i]) && negativeList[i] in arr[..] // All negative numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsNegative(arr[i]) ==> arr[i] in negativeList { negativeList := []; for i := 0 to arr.Length { if IsNegative(arr[i]) { negativeList := negativeList + [arr[i]]; } } }
558
dafny-synthesis_task_id_441.dfy
method CubeSurfaceArea(size: int) returns (area: int) requires size > 0 ensures area == 6 * size * size { area := 6 * size * size; }
method CubeSurfaceArea(size: int) returns (area: int) requires size > 0 ensures area == 6 * size * size { area := 6 * size * size; }
559
dafny-synthesis_task_id_445.dfy
method MultiplyElements(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] * b[i] { result := []; var i := 0; while i < |a| invariant 0 <= i <= |a| invariant |result| == i invariant forall k :: 0 <= k < i ==> result[k] == a[k] * b[k] { result := result + [a[i] * b[i]]; i := i + 1; } }
method MultiplyElements(a: seq<int>, b: seq<int>) returns (result: seq<int>) requires |a| == |b| ensures |result| == |a| ensures forall i :: 0 <= i < |result| ==> result[i] == a[i] * b[i] { result := []; var i := 0; while i < |a| { result := result + [a[i] * b[i]]; i := i + 1; } }
560
dafny-synthesis_task_id_447.dfy
method CubeElements(a: array<int>) returns (cubed: array<int>) ensures cubed.Length == a.Length ensures forall i :: 0 <= i < a.Length ==> cubed[i] == a[i] * a[i] * a[i] { var cubedArray := new int[a.Length]; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant cubedArray.Length == a.Length invariant forall k :: 0 <= k < i ==> cubedArray[k] == a[k] * a[k] * a[k] { cubedArray[i] := a[i] * a[i] * a[i]; } return cubedArray; }
method CubeElements(a: array<int>) returns (cubed: array<int>) ensures cubed.Length == a.Length ensures forall i :: 0 <= i < a.Length ==> cubed[i] == a[i] * a[i] * a[i] { var cubedArray := new int[a.Length]; for i := 0 to a.Length { cubedArray[i] := a[i] * a[i] * a[i]; } return cubedArray; }
561
dafny-synthesis_task_id_452.dfy
method CalculateLoss(costPrice: int, sellingPrice: int) returns (loss: int) requires costPrice >= 0 && sellingPrice >= 0 ensures (costPrice > sellingPrice ==> loss == costPrice - sellingPrice) && (costPrice <= sellingPrice ==> loss == 0) { if (costPrice > sellingPrice) { loss := costPrice - sellingPrice; } else { loss := 0; } }
method CalculateLoss(costPrice: int, sellingPrice: int) returns (loss: int) requires costPrice >= 0 && sellingPrice >= 0 ensures (costPrice > sellingPrice ==> loss == costPrice - sellingPrice) && (costPrice <= sellingPrice ==> loss == 0) { if (costPrice > sellingPrice) { loss := costPrice - sellingPrice; } else { loss := 0; } }
562
dafny-synthesis_task_id_454.dfy
method ContainsZ(s: string) returns (result: bool) ensures result <==> (exists i :: 0 <= i < |s| && (s[i] == 'z' || s[i] == 'Z')) { result := false; for i := 0 to |s| invariant 0 <= i <= |s| invariant result <==> (exists k :: 0 <= k < i && (s[k] == 'z' || s[k] == 'Z')) { if s[i] == 'z' || s[i] == 'Z' { result := true; break; } } }
method ContainsZ(s: string) returns (result: bool) ensures result <==> (exists i :: 0 <= i < |s| && (s[i] == 'z' || s[i] == 'Z')) { result := false; for i := 0 to |s| { if s[i] == 'z' || s[i] == 'Z' { result := true; break; } } }
563
dafny-synthesis_task_id_455.dfy
method MonthHas31Days(month: int) returns (result: bool) requires 1 <= month <= 12 ensures result <==> month in {1, 3, 5, 7, 8, 10, 12} { result := month in {1, 3, 5, 7, 8, 10, 12}; }
method MonthHas31Days(month: int) returns (result: bool) requires 1 <= month <= 12 ensures result <==> month in {1, 3, 5, 7, 8, 10, 12} { result := month in {1, 3, 5, 7, 8, 10, 12}; }
564
dafny-synthesis_task_id_457.dfy
method MinLengthSublist(s: seq<seq<int>>) returns (minSublist: seq<int>) requires |s| > 0 ensures minSublist in s ensures forall sublist :: sublist in s ==> |minSublist| <= |sublist| { minSublist := s[0]; for i := 1 to |s| invariant 0 <= i <= |s| invariant minSublist in s[..i] invariant forall sublist :: sublist in s[..i] ==> |minSublist| <= |sublist| { if |s[i]| < |minSublist| { minSublist := s[i]; } } }
method MinLengthSublist(s: seq<seq<int>>) returns (minSublist: seq<int>) requires |s| > 0 ensures minSublist in s ensures forall sublist :: sublist in s ==> |minSublist| <= |sublist| { minSublist := s[0]; for i := 1 to |s| { if |s[i]| < |minSublist| { minSublist := s[i]; } } }
565
dafny-synthesis_task_id_458.dfy
method RectangleArea(length: int, width: int) returns (area: int) requires length > 0 requires width > 0 ensures area == length * width { area := length * width; }
method RectangleArea(length: int, width: int) returns (area: int) requires length > 0 requires width > 0 ensures area == length * width { area := length * width; }
566
dafny-synthesis_task_id_460.dfy
method GetFirstElements(lst: seq<seq<int>>) returns (result: seq<int>) requires forall i :: 0 <= i < |lst| ==> |lst[i]| > 0 ensures |result| == |lst| ensures forall i :: 0 <= i < |result| ==> result[i] == lst[i][0] { result := []; for i := 0 to |lst| invariant 0 <= i <= |lst| invariant |result| == i invariant forall j :: 0 <= j < i ==> result[j] == lst[j][0] { result := result + [lst[i][0]]; } }
method GetFirstElements(lst: seq<seq<int>>) returns (result: seq<int>) requires forall i :: 0 <= i < |lst| ==> |lst[i]| > 0 ensures |result| == |lst| ensures forall i :: 0 <= i < |result| ==> result[i] == lst[i][0] { result := []; for i := 0 to |lst| { result := result + [lst[i][0]]; } }
567
dafny-synthesis_task_id_461.dfy
predicate IsUpperCase(c: char) { 65 <= c as int <= 90 } method CountUppercase(s: string) returns (count: int) ensures count >= 0 ensures count == | set i: int | 0 <= i < |s| && IsUpperCase(s[i])| { var uppercase := set i: int | 0 <= i < |s| && IsUpperCase(s[i]); count := |uppercase|; }
predicate IsUpperCase(c: char) { 65 <= c as int <= 90 } method CountUppercase(s: string) returns (count: int) ensures count >= 0 ensures count == | set i: int | 0 <= i < |s| && IsUpperCase(s[i])| { var uppercase := set i: int | 0 <= i < |s| && IsUpperCase(s[i]); count := |uppercase|; }
568
dafny-synthesis_task_id_470.dfy
method PairwiseAddition(a: array<int>) returns (result: array<int>) requires a != null requires a.Length % 2 == 0 ensures result != null ensures result.Length == a.Length / 2 ensures forall i :: 0 <= i < result.Length ==> result[i] == a[2*i] + a[2*i + 1] { result := new int[a.Length / 2]; var i := 0; while i < a.Length / 2 invariant 0 <= i <= a.Length / 2 invariant result.Length == a.Length / 2 invariant forall k :: 0 <= k < i ==> result[k] == a[2*k] + a[2*k + 1] { result[i] := a[2*i] + a[2*i + 1]; i := i + 1; } }
method PairwiseAddition(a: array<int>) returns (result: array<int>) requires a != null requires a.Length % 2 == 0 ensures result != null ensures result.Length == a.Length / 2 ensures forall i :: 0 <= i < result.Length ==> result[i] == a[2*i] + a[2*i + 1] { result := new int[a.Length / 2]; var i := 0; while i < a.Length / 2 { result[i] := a[2*i] + a[2*i + 1]; i := i + 1; } }
569
dafny-synthesis_task_id_472.dfy
method ContainsConsecutiveNumbers(a: array<int>) returns (result: bool) requires a.Length>0 ensures result <==> (exists i :: 0 <= i < a.Length - 1 && a[i] + 1 == a[i + 1]) { result := false; for i := 0 to a.Length - 1 invariant 0 <= i <= a.Length - 1 invariant result <==> (exists k :: 0 <= k < i && a[k] + 1 == a[k + 1]) { if a[i] + 1 == a[i + 1] { result := true; break; } } }
method ContainsConsecutiveNumbers(a: array<int>) returns (result: bool) requires a.Length>0 ensures result <==> (exists i :: 0 <= i < a.Length - 1 && a[i] + 1 == a[i + 1]) { result := false; for i := 0 to a.Length - 1 { if a[i] + 1 == a[i + 1] { result := true; break; } } }
570
dafny-synthesis_task_id_474.dfy
method ReplaceChars(s: string, oldChar: char, newChar: char) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> (s[i] == oldChar ==> v[i] == newChar) && (s[i] != oldChar ==> v[i] == s[i]) { var s' : string := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |s'| == i invariant forall k :: 0 <= k < i ==> (s[k] == oldChar ==> s'[k] == newChar) && (s[k] != oldChar ==> s'[k] == s[k]) { if s[i] == oldChar { s' := s' + [newChar]; } else { s' := s' + [s[i]]; } } return s'; }
method ReplaceChars(s: string, oldChar: char, newChar: char) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> (s[i] == oldChar ==> v[i] == newChar) && (s[i] != oldChar ==> v[i] == s[i]) { var s' : string := []; for i := 0 to |s| { if s[i] == oldChar { s' := s' + [newChar]; } else { s' := s' + [s[i]]; } } return s'; }
571
dafny-synthesis_task_id_476.dfy
method SumMinMax(a: array<int>) returns (sum: int) requires a.Length > 0 ensures sum == Max(a[..]) + Min(a[..]) { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length invariant 1 <= i <= a.Length invariant minVal <= maxVal invariant forall k :: 0 <= k < i ==> minVal <= a[k] && a[k] <= maxVal invariant minVal == Min(a[..i]) invariant maxVal == Max(a[..i]) { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } assert a[..i+1][..i] == a[..i]; } assert a[..a.Length] == a[..]; sum := minVal + maxVal; } // The order of the recursion in these two functions // must match the order of the iteration in the algorithm above function Min(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var minPrefix := Min(a[..|a|-1]); if a[|a|-1] <= minPrefix then a[|a|-1] else Min(a[..|a|-1]) } function Max(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var maxPrefix := Max(a[..|a|-1]); if a[|a|-1] >= maxPrefix then a[|a|-1] else Max(a[..|a|-1]) }
method SumMinMax(a: array<int>) returns (sum: int) requires a.Length > 0 ensures sum == Max(a[..]) + Min(a[..]) { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } } sum := minVal + maxVal; } // The order of the recursion in these two functions // must match the order of the iteration in the algorithm above function Min(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var minPrefix := Min(a[..|a|-1]); if a[|a|-1] <= minPrefix then a[|a|-1] else Min(a[..|a|-1]) } function Max(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var maxPrefix := Max(a[..|a|-1]); if a[|a|-1] >= maxPrefix then a[|a|-1] else Max(a[..|a|-1]) }
572
dafny-synthesis_task_id_477.dfy
predicate IsUpperCase(c : char) { 65 <= c as int <= 90 } predicate IsUpperLowerPair(C : char, c : char) { (C as int) == (c as int) - 32 } function Shift32(c : char) : char { ((c as int + 32) % 128) as char } method ToLowercase(s: string) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> if IsUpperCase(s[i]) then IsUpperLowerPair(s[i], v[i]) else v[i] == s[i] { var s' : string := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |s'| == i invariant forall k :: 0 <= k < i && IsUpperCase(s[k]) ==> IsUpperLowerPair(s[k], s'[k]) invariant forall k :: 0 <= k < i && !IsUpperCase(s[k]) ==> s[k] == s'[k] { if IsUpperCase(s[i]) { s' := s' + [Shift32(s[i])]; } else { s' := s' + [s[i]]; } } return s'; }
predicate IsUpperCase(c : char) { 65 <= c as int <= 90 } predicate IsUpperLowerPair(C : char, c : char) { (C as int) == (c as int) - 32 } function Shift32(c : char) : char { ((c as int + 32) % 128) as char } method ToLowercase(s: string) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> if IsUpperCase(s[i]) then IsUpperLowerPair(s[i], v[i]) else v[i] == s[i] { var s' : string := []; for i := 0 to |s| { if IsUpperCase(s[i]) { s' := s' + [Shift32(s[i])]; } else { s' := s' + [s[i]]; } } return s'; }
573
dafny-synthesis_task_id_554.dfy
/** * Find odd numbers from an array of numbers **/ predicate IsOdd(n: int) { n % 2 == 1 } method FindOddNumbers(arr: array<int>) returns (oddList: seq<int>) // All numbers in the output are odd and exist in the input ensures forall i :: 0 <= i < |oddList| ==> IsOdd(oddList[i]) && oddList[i] in arr[..] // All odd numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsOdd(arr[i]) ==> arr[i] in oddList { oddList := []; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant 0 <= |oddList| <= i invariant forall k :: 0 <= k < |oddList| ==> IsOdd(oddList[k]) && oddList[k] in arr[..] invariant forall k :: 0 <= k < i && IsOdd(arr[k]) ==> arr[k] in oddList { if IsOdd(arr[i]) { oddList := oddList + [arr[i]]; } } }
/** * Find odd numbers from an array of numbers **/ predicate IsOdd(n: int) { n % 2 == 1 } method FindOddNumbers(arr: array<int>) returns (oddList: seq<int>) // All numbers in the output are odd and exist in the input ensures forall i :: 0 <= i < |oddList| ==> IsOdd(oddList[i]) && oddList[i] in arr[..] // All odd numbers in the input are in the output ensures forall i :: 0 <= i < arr.Length && IsOdd(arr[i]) ==> arr[i] in oddList { oddList := []; for i := 0 to arr.Length { if IsOdd(arr[i]) { oddList := oddList + [arr[i]]; } } }
574
dafny-synthesis_task_id_555.dfy
method DifferenceSumCubesAndSumNumbers(n: int) returns (diff: int) requires n >= 0 ensures diff == (n * n * (n + 1) * (n + 1)) / 4 - (n * (n + 1)) / 2 { var sumCubes := 0; var sumNumbers := 0; for i := 1 to n + 1 invariant 0 <= i <= n + 1 invariant sumCubes == (i - 1) * (i - 1) * i * i / 4 invariant sumNumbers == (i - 1) * i / 2 { sumCubes := sumCubes + i * i * i; sumNumbers := sumNumbers + i; } diff := sumCubes - sumNumbers; }
method DifferenceSumCubesAndSumNumbers(n: int) returns (diff: int) requires n >= 0 ensures diff == (n * n * (n + 1) * (n + 1)) / 4 - (n * (n + 1)) / 2 { var sumCubes := 0; var sumNumbers := 0; for i := 1 to n + 1 { sumCubes := sumCubes + i * i * i; sumNumbers := sumNumbers + i; } diff := sumCubes - sumNumbers; }
575
dafny-synthesis_task_id_557.dfy
predicate IsLowerCase(c : char) { 97 <= c as int <= 122 } predicate IsUpperCase(c : char) { 65 <= c as int <= 90 } predicate IsLowerUpperPair(c : char, C : char) { (c as int) == (C as int) + 32 } predicate IsUpperLowerPair(C : char, c : char) { (C as int) == (c as int) - 32 } function ShiftMinus32(c : char) : char { ((c as int - 32) % 128) as char } function Shift32(c : char) : char { ((c as int + 32) % 128) as char } method ToggleCase(s: string) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> if IsLowerCase(s[i]) then IsLowerUpperPair(s[i], v[i]) else if IsUpperCase(s[i]) then IsUpperLowerPair(s[i], v[i]) else v[i] == s[i] { var s' : string := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |s'| == i invariant forall k :: 0 <= k < i && IsLowerCase(s[k]) ==> IsLowerUpperPair(s[k], s'[k]) invariant forall k :: 0 <= k < i && IsUpperCase(s[k]) ==> IsUpperLowerPair(s[k], s'[k]) invariant forall k :: 0 <= k < i && !IsLowerCase(s[k]) && !IsUpperCase(s[k]) ==> s[k] == s'[k] { if IsLowerCase(s[i]) { s' := s' + [ShiftMinus32(s[i])]; } else if IsUpperCase(s[i]) { s' := s' + [Shift32(s[i])]; } else { s' := s' + [s[i]]; } } return s'; }
predicate IsLowerCase(c : char) { 97 <= c as int <= 122 } predicate IsUpperCase(c : char) { 65 <= c as int <= 90 } predicate IsLowerUpperPair(c : char, C : char) { (c as int) == (C as int) + 32 } predicate IsUpperLowerPair(C : char, c : char) { (C as int) == (c as int) - 32 } function ShiftMinus32(c : char) : char { ((c as int - 32) % 128) as char } function Shift32(c : char) : char { ((c as int + 32) % 128) as char } method ToggleCase(s: string) returns (v: string) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> if IsLowerCase(s[i]) then IsLowerUpperPair(s[i], v[i]) else if IsUpperCase(s[i]) then IsUpperLowerPair(s[i], v[i]) else v[i] == s[i] { var s' : string := []; for i := 0 to |s| { if IsLowerCase(s[i]) { s' := s' + [ShiftMinus32(s[i])]; } else if IsUpperCase(s[i]) { s' := s' + [Shift32(s[i])]; } else { s' := s' + [s[i]]; } } return s'; }
576
dafny-synthesis_task_id_565.dfy
method SplitStringIntoChars(s: string) returns (v: seq<char>) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> v[i] == s[i] { v := []; for i := 0 to |s| invariant 0 <= i <= |s| invariant |v| == i invariant forall k :: 0 <= k < i ==> v[k] == s[k] { v := v + [s[i]]; } }
method SplitStringIntoChars(s: string) returns (v: seq<char>) ensures |v| == |s| ensures forall i :: 0 <= i < |s| ==> v[i] == s[i] { v := []; for i := 0 to |s| { v := v + [s[i]]; } }
577
dafny-synthesis_task_id_566.dfy
method SumOfDigits(number: nat) returns (sum: nat) requires number >= 0 ensures sum >= 0 ensures sum == SumDigits(number) { sum := 0; var n: nat := number; // Let's find out the number of digits, which is the same as powers of ten for the given number ghost var ndigits := NumberOfDigits(number); X(number); assert Power10(ndigits) > number; ghost var PowersOfTen := seq(ndigits+1, i requires 0 <= i <= ndigits => Power10(i)); ghost var pmax := Power10(ndigits); ghost var p := PowersOfTen[0]; assert pmax == PowersOfTen[|PowersOfTen|-1]; assert pmax > number; // Let's compute the values of n ghost var ValuesOfn := seq(ndigits+1, i requires 0 <= i <= ndigits => number / PowersOfTen[i]); assert forall j :: 0 < j <= ndigits ==> ValuesOfn[j] == ValuesOfn[j-1]/10; assert ValuesOfn[0] == number; //DivIsZero(); assert ValuesOfn[|ValuesOfn|-1] == number/pmax == 0; assert ValuesOfn[0] == n; assert PowersOfTen[0] == p; ghost var i := 0; while n > 0 // invariant 1 <= p <= pmax // invariant n in ValuesOfn invariant 0 <= i <= ndigits invariant ValuesOfn[i] == n invariant PowersOfTen[i] == p invariant sum >= 0 invariant sum == SumDigits(number % p) { assert ValuesOfn[i] == n; var digit := n % 10; sum := sum + digit; n := n / 10; i := i + 1; // assert ValuesOfn[i] == ValuesOfn[i-1]/10; // assert ValuesOfn[i] == n; p := PowersOfTen[i]; //p * 10; assert n == 0 ==> p == pmax; } assert n == 0; // assert i == ndigits; assert p == pmax; NumberIdentity(number, p); assert number == number % p; } //lemma DivIsZero() // ensures forall num, den : nat :: den >= 1 && num < den ==> num/den == 0 lemma X(x: nat) ensures Power10(NumberOfDigits(x)) > x { if x <= 9 { assert NumberOfDigits(x) == 1; assert Power10(NumberOfDigits(x)) == 10; assert Power10(NumberOfDigits(x)) > x; } else // >= 10 { assert NumberOfDigits(x) >= 2; X(x/10); assert NumberOfDigits(x) == NumberOfDigits(x/10) + 1; assert Power10(NumberOfDigits(x)) == Power10(NumberOfDigits(x/10)) * 10; assert Power10(NumberOfDigits(x)) > x; } } lemma NumberIdentity(number: nat, pmax: nat) requires pmax == Power10(NumberOfDigits(number)) ensures number == number % pmax { if NumberOfDigits(number) == 1 { assert 0 <= number <= 9; assert pmax == 10; assert number == number % pmax; } else // > 1 { assert pmax == Power10(NumberOfDigits(number)) ==> pmax/10 == Power10(NumberOfDigits(number/10)); NumberIdentity(number/10, pmax/10); assert number >= 10; assert pmax >= 100; assert number < pmax; assert forall n, m :: 0 < n < m <= pmax ==> n%m == n; assert number == number % pmax; } } lemma InIntValues(n: nat) ensures 0 in IntValues(n) ensures n in IntValues(n) ensures n/10 in IntValues(n) {} // ghost function ValuesOfn(number: nat, ndigits: nat) : (r: seq<nat>) // { // seq(ndigits+1, i requires 0 <= i <= ndigits => number / PowersOfTen[i]) // } ghost function IntValues(n: int) : (r: seq<int>) requires n >= 0 ensures 0 in r ensures n in r ensures n/10 in r // ensures forall p :: p in powersOfTen ==> n/p in r { if n == 0 then [0] else [n] + IntValues(n/10) } function Power10(n: nat): (r: nat) ensures r >= 1 ensures n > 0 ==> r % 10 == 0 { if (n == 0) then 1 else 10 * Power10(n-1) } function NumberToSeq(number: int) : seq<int> requires number >= 0 { if number == 0 then [] else [number % 10] + NumberToSeq(number/10) } function Sum(digits: seq<int>) : int { if |digits| == 0 then 0 else digits[0] + Sum(digits[1..]) } function SumDigits(n: nat) : nat { var ndigits := NumberOfDigits(n); var p := Power10(ndigits-1); SumDigitsRecursive(n, p) } function SumDigitsRecursive(n: nat, p: nat) : (r: nat) { if n == 0 || p == 0 then 0 else var leftMostDigit := n/p; var rest := n%p; leftMostDigit + SumDigitsRecursive(rest, p/10) } function NumberOfDigits(n: nat) : (r: nat) ensures r >= 1 ensures r == 1 <==> 0 <= n <= 9 { if 0 <= n <= 9 then 1 else 1+NumberOfDigits(n/10) }
method SumOfDigits(number: nat) returns (sum: nat) requires number >= 0 ensures sum >= 0 ensures sum == SumDigits(number) { sum := 0; var n: nat := number; // Let's find out the number of digits, which is the same as powers of ten for the given number ghost var ndigits := NumberOfDigits(number); X(number); ghost var PowersOfTen := seq(ndigits+1, i requires 0 <= i <= ndigits => Power10(i)); ghost var pmax := Power10(ndigits); ghost var p := PowersOfTen[0]; // Let's compute the values of n ghost var ValuesOfn := seq(ndigits+1, i requires 0 <= i <= ndigits => number / PowersOfTen[i]); //DivIsZero(); ghost var i := 0; while n > 0 { var digit := n % 10; sum := sum + digit; n := n / 10; i := i + 1; p := PowersOfTen[i]; //p * 10; } NumberIdentity(number, p); } //lemma DivIsZero() // ensures forall num, den : nat :: den >= 1 && num < den ==> num/den == 0 lemma X(x: nat) ensures Power10(NumberOfDigits(x)) > x { if x <= 9 { } else // >= 10 { X(x/10); } } lemma NumberIdentity(number: nat, pmax: nat) requires pmax == Power10(NumberOfDigits(number)) ensures number == number % pmax { if NumberOfDigits(number) == 1 { } else // > 1 { NumberIdentity(number/10, pmax/10); } } lemma InIntValues(n: nat) ensures 0 in IntValues(n) ensures n in IntValues(n) ensures n/10 in IntValues(n) {} // ghost function ValuesOfn(number: nat, ndigits: nat) : (r: seq<nat>) // { // seq(ndigits+1, i requires 0 <= i <= ndigits => number / PowersOfTen[i]) // } ghost function IntValues(n: int) : (r: seq<int>) requires n >= 0 ensures 0 in r ensures n in r ensures n/10 in r // ensures forall p :: p in powersOfTen ==> n/p in r { if n == 0 then [0] else [n] + IntValues(n/10) } function Power10(n: nat): (r: nat) ensures r >= 1 ensures n > 0 ==> r % 10 == 0 { if (n == 0) then 1 else 10 * Power10(n-1) } function NumberToSeq(number: int) : seq<int> requires number >= 0 { if number == 0 then [] else [number % 10] + NumberToSeq(number/10) } function Sum(digits: seq<int>) : int { if |digits| == 0 then 0 else digits[0] + Sum(digits[1..]) } function SumDigits(n: nat) : nat { var ndigits := NumberOfDigits(n); var p := Power10(ndigits-1); SumDigitsRecursive(n, p) } function SumDigitsRecursive(n: nat, p: nat) : (r: nat) { if n == 0 || p == 0 then 0 else var leftMostDigit := n/p; var rest := n%p; leftMostDigit + SumDigitsRecursive(rest, p/10) } function NumberOfDigits(n: nat) : (r: nat) ensures r >= 1 ensures r == 1 <==> 0 <= n <= 9 { if 0 <= n <= 9 then 1 else 1+NumberOfDigits(n/10) }
578
dafny-synthesis_task_id_567.dfy
method IsSorted(a: array<int>) returns (sorted: bool) requires a.Length > 0 ensures sorted <== forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures !sorted ==> exists i, j :: 0 <= i < j < a.Length && a[i] > a[j] { sorted := true; for i := 0 to a.Length - 1 invariant 0 <= i < a.Length invariant sorted <== forall k, l :: 0 <= k < l < i ==> a[k] <= a[l] invariant !sorted ==> exists k :: 0 <= k < i && a[k] > a[k+1] { if a[i] > a[i + 1] { sorted := false; break; } } sorted := sorted; }
method IsSorted(a: array<int>) returns (sorted: bool) requires a.Length > 0 ensures sorted <== forall i, j :: 0 <= i < j < a.Length ==> a[i] <= a[j] ensures !sorted ==> exists i, j :: 0 <= i < j < a.Length && a[i] > a[j] { sorted := true; for i := 0 to a.Length - 1 { if a[i] > a[i + 1] { sorted := false; break; } } sorted := sorted; }
579
dafny-synthesis_task_id_572.dfy
method RemoveDuplicates(a: array<int>) returns (result: seq<int>) requires a != null ensures forall x :: x in result <==> exists i :: 0 <= i < a.Length && a[i] == x ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant forall x :: x in res <==> exists k :: 0 <= k < i && a[k] == x invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if a[i] !in res { res := res + [a[i]]; } } result := res; }
method RemoveDuplicates(a: array<int>) returns (result: seq<int>) requires a != null ensures forall x :: x in result <==> exists i :: 0 <= i < a.Length && a[i] == x ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length { if a[i] !in res { res := res + [a[i]]; } } result := res; }
580
dafny-synthesis_task_id_573.dfy
method UniqueProduct (arr: array<int>) returns (product: int) ensures product == SetProduct((set i | 0 <= i < arr.Length :: arr[i])) { var p := 1; var seen: set<int> := {}; for i := 0 to arr.Length invariant 0 <= i <= arr.Length invariant seen == (set k | 0 <= k < i :: arr[k]) invariant p == SetProduct((set k | 0 <= k < i :: arr[k])) { if ! (arr[i] in seen) { seen := seen + { arr[i] }; p := p * arr[i]; SetProductLemma(seen, arr[i]); } } product := p; } ghost function SetProduct(s : set<int>) : int { if s == {} then 1 else var x :| x in s; x * SetProduct(s - {x}) } /* * This is necessary because, when we add one element, we need to make sure * that the product of the new set, as a whole, is the same as the product * of the old set times the new element. */ lemma SetProductLemma(s: set <int>, x: int) requires x in s ensures SetProduct(s - {x}) * x == SetProduct(s) { if s != {} { var y :| y in s && y * SetProduct(s - {y}) == SetProduct(s); if y == x {} else { calc { SetProduct(s); y * SetProduct(s - {y}); { SetProductLemma(s - {y}, x); } y * x * SetProduct(s - {y} - {x}); { assert s - {x} - {y} == s - {y} - {x};} y * x * SetProduct(s - {x} - {y}); x * y * SetProduct(s - {x} - {y}); { SetProductLemma(s - {x}, y); } x * SetProduct(s - {x}); } } } }
method UniqueProduct (arr: array<int>) returns (product: int) ensures product == SetProduct((set i | 0 <= i < arr.Length :: arr[i])) { var p := 1; var seen: set<int> := {}; for i := 0 to arr.Length { if ! (arr[i] in seen) { seen := seen + { arr[i] }; p := p * arr[i]; SetProductLemma(seen, arr[i]); } } product := p; } ghost function SetProduct(s : set<int>) : int { if s == {} then 1 else var x :| x in s; x * SetProduct(s - {x}) } /* * This is necessary because, when we add one element, we need to make sure * that the product of the new set, as a whole, is the same as the product * of the old set times the new element. */ lemma SetProductLemma(s: set <int>, x: int) requires x in s ensures SetProduct(s - {x}) * x == SetProduct(s) { if s != {} { var y :| y in s && y * SetProduct(s - {y}) == SetProduct(s); if y == x {} else { calc { SetProduct(s); y * SetProduct(s - {y}); { SetProductLemma(s - {y}, x); } y * x * SetProduct(s - {y} - {x}); y * x * SetProduct(s - {x} - {y}); x * y * SetProduct(s - {x} - {y}); { SetProductLemma(s - {x}, y); } x * SetProduct(s - {x}); } } } }
581
dafny-synthesis_task_id_574.dfy
method CylinderSurfaceArea(radius: real, height: real) returns (area: real) requires radius > 0.0 && height > 0.0 ensures area == 2.0 * 3.14159265358979323846 * radius * (radius + height) { area := 2.0 * 3.14159265358979323846 * radius * (radius + height); }
method CylinderSurfaceArea(radius: real, height: real) returns (area: real) requires radius > 0.0 && height > 0.0 ensures area == 2.0 * 3.14159265358979323846 * radius * (radius + height) { area := 2.0 * 3.14159265358979323846 * radius * (radius + height); }
582
dafny-synthesis_task_id_576.dfy
method IsSublist(sub: seq<int>, main: seq<int>) returns (result: bool) ensures true <== (exists i :: 0 <= i <= |main| - |sub| && sub == main[i..i + |sub|]) { if |sub| > |main| { return false; } for i := 0 to |main| - |sub| + 1 invariant 0 <= i <= |main| - |sub| + 1 invariant true <== (exists j :: 0 <= j < i && sub == main[j..j + |sub|]) { if sub == main[i..i + |sub|] { result := true; } } result := false; }
method IsSublist(sub: seq<int>, main: seq<int>) returns (result: bool) ensures true <== (exists i :: 0 <= i <= |main| - |sub| && sub == main[i..i + |sub|]) { if |sub| > |main| { return false; } for i := 0 to |main| - |sub| + 1 { if sub == main[i..i + |sub|] { result := true; } } result := false; }
583
dafny-synthesis_task_id_577.dfy
function Factorial(n: int): int requires n >= 0 ensures 0 <= Factorial(n) { if n == 0 then 1 else n * Factorial(n-1) } method FactorialOfLastDigit(n: int) returns (fact: int) requires n >= 0 ensures fact == Factorial(n % 10) { var lastDigit := n % 10; fact := Factorial(lastDigit); }
function Factorial(n: int): int requires n >= 0 ensures 0 <= Factorial(n) { if n == 0 then 1 else n * Factorial(n-1) } method FactorialOfLastDigit(n: int) returns (fact: int) requires n >= 0 ensures fact == Factorial(n % 10) { var lastDigit := n % 10; fact := Factorial(lastDigit); }
584
dafny-synthesis_task_id_578.dfy
method Interleave(s1: seq<int>, s2: seq<int>, s3: seq<int>) returns (r: seq<int>) requires |s1| == |s2| && |s2| == |s3| ensures |r| == 3 * |s1| ensures forall i :: 0 <= i < |s1| ==> r[3*i] == s1[i] && r[3*i + 1] == s2[i] && r[3*i + 2] == s3[i] { r := []; for i := 0 to |s1| invariant 0 <= i <= |s1| invariant |r| == 3 * i invariant forall k :: 0 <= k < i ==> r[3*k] == s1[k] && r[3*k + 1] == s2[k] && r[3*k + 2] == s3[k] { r := r + [s1[i], s2[i], s3[i]]; } }
method Interleave(s1: seq<int>, s2: seq<int>, s3: seq<int>) returns (r: seq<int>) requires |s1| == |s2| && |s2| == |s3| ensures |r| == 3 * |s1| ensures forall i :: 0 <= i < |s1| ==> r[3*i] == s1[i] && r[3*i + 1] == s2[i] && r[3*i + 2] == s3[i] { r := []; for i := 0 to |s1| { r := r + [s1[i], s2[i], s3[i]]; } }
585
dafny-synthesis_task_id_579.dfy
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method DissimilarElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are either in a or b, but not in both or neither ensures forall x :: x in result ==> (InArray(a, x) != InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant forall x :: x in res ==> InArray(a, x) invariant forall x :: x in res ==> InArray(a, x) != InArray(b, x) invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if !InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } ghost var partialSize := |res|; for i := 0 to b.Length invariant 0 <= i <= b.Length invariant forall k :: partialSize <= k < |res| ==> InArray(b, res[k]) invariant forall k :: 0 <= k < |res| ==> InArray(a, res[k]) != InArray(b, res[k]) invariant forall i, j :: 0 <= i < j < |res| ==> res[i] != res[j] { if !InArray(a, b[i]) && b[i] !in res { res := res + [b[i]]; } } result := res; }
predicate InArray(a: array<int>, x: int) reads a { exists i :: 0 <= i < a.Length && a[i] == x } method DissimilarElements(a: array<int>, b: array<int>) returns (result: seq<int>) // All elements in the output are either in a or b, but not in both or neither ensures forall x :: x in result ==> (InArray(a, x) != InArray(b, x)) // The elements in the output are all different ensures forall i, j :: 0 <= i < j < |result| ==> result[i] != result[j] { var res: seq<int> := []; for i := 0 to a.Length { if !InArray(b, a[i]) && a[i] !in res { res := res + [a[i]]; } } ghost var partialSize := |res|; for i := 0 to b.Length { if !InArray(a, b[i]) && b[i] !in res { res := res + [b[i]]; } } result := res; }
586
dafny-synthesis_task_id_58.dfy
method HasOppositeSign(a: int, b: int) returns (result: bool) ensures result <==> (a < 0 && b > 0) || (a > 0 && b < 0) { result := (a < 0 && b > 0) || (a > 0 && b < 0); }
method HasOppositeSign(a: int, b: int) returns (result: bool) ensures result <==> (a < 0 && b > 0) || (a > 0 && b < 0) { result := (a < 0 && b > 0) || (a > 0 && b < 0); }
587
dafny-synthesis_task_id_581.dfy
method SquarePyramidSurfaceArea(baseEdge: int, height: int) returns (area: int) requires baseEdge > 0 requires height > 0 ensures area == baseEdge * baseEdge + 2 * baseEdge * height { area := baseEdge * baseEdge + 2 * baseEdge * height; }
method SquarePyramidSurfaceArea(baseEdge: int, height: int) returns (area: int) requires baseEdge > 0 requires height > 0 ensures area == baseEdge * baseEdge + 2 * baseEdge * height { area := baseEdge * baseEdge + 2 * baseEdge * height; }
588
dafny-synthesis_task_id_586.dfy
method SplitAndAppend(l: seq<int>, n: int) returns (r: seq<int>) requires n >= 0 && n < |l| ensures |r| == |l| ensures forall i :: 0 <= i < |l| ==> r[i] == l[(i + n) % |l|] { var firstPart: seq<int> := l[..n]; var secondPart: seq<int> := l[n..]; r := secondPart + firstPart; }
method SplitAndAppend(l: seq<int>, n: int) returns (r: seq<int>) requires n >= 0 && n < |l| ensures |r| == |l| ensures forall i :: 0 <= i < |l| ==> r[i] == l[(i + n) % |l|] { var firstPart: seq<int> := l[..n]; var secondPart: seq<int> := l[n..]; r := secondPart + firstPart; }
589
dafny-synthesis_task_id_587.dfy
method ArrayToSeq(a: array<int>) returns (s: seq<int>) requires a != null ensures |s| == a.Length ensures forall i :: 0 <= i < a.Length ==> s[i] == a[i] { s := []; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant |s| == i invariant forall j :: 0 <= j < i ==> s[j] == a[j] { s := s + [a[i]]; } }
method ArrayToSeq(a: array<int>) returns (s: seq<int>) requires a != null ensures |s| == a.Length ensures forall i :: 0 <= i < a.Length ==> s[i] == a[i] { s := []; for i := 0 to a.Length { s := s + [a[i]]; } }
590
dafny-synthesis_task_id_588.dfy
method DifferenceMinMax(a: array<int>) returns (diff: int) requires a.Length > 0 ensures diff == Max(a[..]) - Min(a[..]) { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length invariant 1 <= i <= a.Length invariant minVal <= maxVal invariant forall k :: 0 <= k < i ==> minVal <= a[k] && a[k] <= maxVal invariant minVal == Min(a[..i]) invariant maxVal == Max(a[..i]) { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } assert a[..i+1][..i] == a[..i]; } assert a[..a.Length] == a[..]; diff := maxVal - minVal; } // The order of the recursion in these two functions // must match the order of the iteration in the algorithm above function Min(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var minPrefix := Min(a[..|a|-1]); if a[|a|-1] <= minPrefix then a[|a|-1] else Min(a[..|a|-1]) } function Max(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var maxPrefix := Max(a[..|a|-1]); if a[|a|-1] >= maxPrefix then a[|a|-1] else Max(a[..|a|-1]) }
method DifferenceMinMax(a: array<int>) returns (diff: int) requires a.Length > 0 ensures diff == Max(a[..]) - Min(a[..]) { var minVal := a[0]; var maxVal := a[0]; for i := 1 to a.Length { if a[i] < minVal { minVal := a[i]; } else if a[i] > maxVal { maxVal := a[i]; } } diff := maxVal - minVal; } // The order of the recursion in these two functions // must match the order of the iteration in the algorithm above function Min(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var minPrefix := Min(a[..|a|-1]); if a[|a|-1] <= minPrefix then a[|a|-1] else Min(a[..|a|-1]) } function Max(a: seq<int>) : int requires |a| > 0 { if |a| == 1 then a[0] else var maxPrefix := Max(a[..|a|-1]); if a[|a|-1] >= maxPrefix then a[|a|-1] else Max(a[..|a|-1]) }
591
dafny-synthesis_task_id_59.dfy
method NthOctagonalNumber(n: int) returns (octagonalNumber: int) requires n >= 0 ensures octagonalNumber == n * (3 * n - 2) { octagonalNumber := n * (3 * n - 2); }
method NthOctagonalNumber(n: int) returns (octagonalNumber: int) requires n >= 0 ensures octagonalNumber == n * (3 * n - 2) { octagonalNumber := n * (3 * n - 2); }
592
dafny-synthesis_task_id_591.dfy
method SwapFirstAndLast(a: array<int>) requires a != null && a.Length > 0 modifies a ensures a[0] == old(a[a.Length - 1]) && a[a.Length - 1] == old(a[0]) ensures forall k :: 1 <= k < a.Length - 1 ==> a[k] == old(a[k]) { var temp := a[0]; a[0] := a[a.Length - 1]; a[a.Length - 1] := temp; }
method SwapFirstAndLast(a: array<int>) requires a != null && a.Length > 0 modifies a ensures a[0] == old(a[a.Length - 1]) && a[a.Length - 1] == old(a[0]) ensures forall k :: 1 <= k < a.Length - 1 ==> a[k] == old(a[k]) { var temp := a[0]; a[0] := a[a.Length - 1]; a[a.Length - 1] := temp; }
593
dafny-synthesis_task_id_594.dfy
predicate IsEven(n: int) { n % 2 == 0 } predicate IsOdd(n: int) { n % 2 != 0 } method FirstEvenOddDifference(a: array<int>) returns (diff: int) requires a.Length >= 2 requires exists i :: 0 <= i < a.Length && IsEven(a[i]) requires exists i :: 0 <= i < a.Length && IsOdd(a[i]) ensures exists i, j :: 0 <= i < a.Length && 0 <= j < a.Length && IsEven(a[i]) && IsOdd(a[j]) && diff == a[i] - a[j] && (forall k :: 0 <= k < i ==> IsOdd(a[k])) && (forall k :: 0 <= k < j ==> IsEven(a[k])) { var firstEven: int := -1; var firstOdd: int := -1; for i := 0 to a.Length invariant 0 <= i <= a.Length invariant (firstEven == -1 || (0 <= firstEven < i && IsEven(a[firstEven]))) invariant (firstOdd == -1 || (0 <= firstOdd < i && IsOdd(a[firstOdd]))) invariant firstEven == -1 ==> (forall k :: 0 <= k < i ==> !IsEven(a[k])) invariant firstOdd == -1 ==> (forall k :: 0 <= k < i ==> !IsOdd(a[k])) invariant firstEven != -1 ==> (forall k :: 0 <= k < firstEven ==> IsOdd(a[k])) invariant firstOdd != -1 ==> (forall k :: 0 <= k < firstOdd ==> IsEven(a[k])) { if firstEven == -1 && IsEven(a[i]) { firstEven := i; } if firstOdd == -1 && IsOdd(a[i]) { firstOdd := i; } if firstEven != -1 && firstOdd != -1 { break; } } diff := a[firstEven] - a[firstOdd]; }
predicate IsEven(n: int) { n % 2 == 0 } predicate IsOdd(n: int) { n % 2 != 0 } method FirstEvenOddDifference(a: array<int>) returns (diff: int) requires a.Length >= 2 requires exists i :: 0 <= i < a.Length && IsEven(a[i]) requires exists i :: 0 <= i < a.Length && IsOdd(a[i]) ensures exists i, j :: 0 <= i < a.Length && 0 <= j < a.Length && IsEven(a[i]) && IsOdd(a[j]) && diff == a[i] - a[j] && (forall k :: 0 <= k < i ==> IsOdd(a[k])) && (forall k :: 0 <= k < j ==> IsEven(a[k])) { var firstEven: int := -1; var firstOdd: int := -1; for i := 0 to a.Length { if firstEven == -1 && IsEven(a[i]) { firstEven := i; } if firstOdd == -1 && IsOdd(a[i]) { firstOdd := i; } if firstEven != -1 && firstOdd != -1 { break; } } diff := a[firstEven] - a[firstOdd]; }
594
dafny-synthesis_task_id_598.dfy
method IsArmstrong(n: int) returns (result: bool) requires 100 <= n < 1000 ensures result <==> (n == ((n / 100) * (n / 100) * (n / 100) + ((n / 10) % 10) * ((n / 10) % 10) * ((n / 10) % 10) + (n % 10) * (n % 10) * (n % 10))) { var a := n / 100; var b := (n / 10) % 10; var c := n % 10; result := n == (a * a * a + b * b * b + c * c * c); }
method IsArmstrong(n: int) returns (result: bool) requires 100 <= n < 1000 ensures result <==> (n == ((n / 100) * (n / 100) * (n / 100) + ((n / 10) % 10) * ((n / 10) % 10) * ((n / 10) % 10) + (n % 10) * (n % 10) * (n % 10))) { var a := n / 100; var b := (n / 10) % 10; var c := n % 10; result := n == (a * a * a + b * b * b + c * c * c); }
595
dafny-synthesis_task_id_599.dfy
method SumAndAverage(n: int) returns (sum: int, average: real) requires n > 0 ensures sum == n * (n + 1) / 2 ensures average == sum as real / n as real { sum := 0; for i := 1 to n + 1 invariant 0 <= i <= n + 1 invariant sum == (i - 1) * i / 2 { sum := sum + i; } average := sum as real / n as real; }
method SumAndAverage(n: int) returns (sum: int, average: real) requires n > 0 ensures sum == n * (n + 1) / 2 ensures average == sum as real / n as real { sum := 0; for i := 1 to n + 1 { sum := sum + i; } average := sum as real / n as real; }
596
dafny-synthesis_task_id_600.dfy
method IsEven(n: int) returns (result: bool) ensures result <==> n % 2 == 0 { result := n % 2 == 0; }
method IsEven(n: int) returns (result: bool) ensures result <==> n % 2 == 0 { result := n % 2 == 0; }
597
dafny-synthesis_task_id_602.dfy
method FindFirstRepeatedChar(s: string) returns (found: bool, c: char) ensures found ==> exists i, j :: 0 <= i < j < |s| && s[i] == s[j] && s[i] == c && (forall k, l :: 0 <= k < l < j && s[k] == s[l] ==> k >= i) ensures !found ==> (forall i, j :: 0 <= i < j < |s| ==> s[i] != s[j]) { c := ' '; found := false; var inner_found := false; var i := 0; while i < |s| && !found invariant 0 <= i <= |s| invariant found == inner_found // Found: there exists number ii less or equal to i, that we looked above it and found it. And, btw, that didn't happen for any number less than ii invariant found ==> exists ii, jj :: 0 <= ii < i && ii < jj < |s| && s[ii] == s[jj] && s[ii] == c && (forall k, l :: 0 <= k < l < jj && s[k] == s[l] ==> k >= ii) // Not found: for every number up to i, we looked above it, and didn't find it invariant !found <==> (forall ii, jj :: 0 <= ii < i && ii < jj < |s| ==> s[ii] != s[jj]) { var j := i + 1; while j < |s| && !inner_found invariant i < j <= |s| invariant inner_found ==> exists k :: i < k < |s| && s[i] == s[k] && s[i] == c invariant !inner_found <==> (forall k :: i < k < j ==> s[i] != s[k]) { if s[i] == s[j] { inner_found := true; c := s[i]; } j := j + 1; } found := inner_found; i := i + 1; } }
method FindFirstRepeatedChar(s: string) returns (found: bool, c: char) ensures found ==> exists i, j :: 0 <= i < j < |s| && s[i] == s[j] && s[i] == c && (forall k, l :: 0 <= k < l < j && s[k] == s[l] ==> k >= i) ensures !found ==> (forall i, j :: 0 <= i < j < |s| ==> s[i] != s[j]) { c := ' '; found := false; var inner_found := false; var i := 0; while i < |s| && !found // Found: there exists number ii less or equal to i, that we looked above it and found it. And, btw, that didn't happen for any number less than ii // Not found: for every number up to i, we looked above it, and didn't find it { var j := i + 1; while j < |s| && !inner_found { if s[i] == s[j] { inner_found := true; c := s[i]; } j := j + 1; } found := inner_found; i := i + 1; } }
598
dafny-synthesis_task_id_603.dfy
method LucidNumbers(n: int) returns (lucid: seq<int>) requires n >= 0 ensures forall i :: 0 <= i < |lucid| ==> lucid[i] % 3 == 0 ensures forall i :: 0 <= i < |lucid| ==> lucid[i] <= n ensures forall i, j :: 0 <= i < j < |lucid| ==> lucid[i] < lucid[j] { lucid := []; var i := 0; while i <= n invariant 0 <= i <= n + 1 invariant forall k :: 0 <= k < |lucid| ==> lucid[k] % 3 == 0 invariant forall k :: 0 <= k < |lucid| ==> lucid[k] <= i - 1 invariant forall k, l :: 0 <= k < l < |lucid| ==> lucid[k] < lucid[l] { if i % 3 == 0 { lucid := lucid + [i]; } i := i + 1; } }
method LucidNumbers(n: int) returns (lucid: seq<int>) requires n >= 0 ensures forall i :: 0 <= i < |lucid| ==> lucid[i] % 3 == 0 ensures forall i :: 0 <= i < |lucid| ==> lucid[i] <= n ensures forall i, j :: 0 <= i < j < |lucid| ==> lucid[i] < lucid[j] { lucid := []; var i := 0; while i <= n { if i % 3 == 0 { lucid := lucid + [i]; } i := i + 1; } }
599
dafny-synthesis_task_id_605.dfy
method IsPrime(n: int) returns (result: bool) requires n >= 2 ensures result <==> (forall k :: 2 <= k < n ==> n % k != 0) { result := true; var i := 2; while i <= n/2 invariant 2 <= i invariant result <==> (forall k :: 2 <= k < i ==> n % k != 0) { if n % i == 0 { result := false; break; } i := i + 1; } }
method IsPrime(n: int) returns (result: bool) requires n >= 2 ensures result <==> (forall k :: 2 <= k < n ==> n % k != 0) { result := true; var i := 2; while i <= n/2 { if n % i == 0 { result := false; break; } i := i + 1; } }