Skip to content

Commit f95c6e0

Browse files
committed
feat: Port 20 more numeric-only Dafny synthesis tasks (batch 7)
1 parent 2b971e7 commit f95c6e0

20 files changed

+442
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Synthesis Task 101: Get the k-th element from an array (1-indexed)
2+
3+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask101
4+
5+
/-- Returns the k-th element from an array (1-indexed) -/
6+
def kthElement (arr : Array Int) (k : Int) : Int :=
7+
sorry
8+
9+
/-- Specification: Returns the element at position k-1 (0-indexed) -/
10+
theorem kthElement_spec (arr : Array Int) (k : Int)
11+
(h : 1 ≤ k ∧ k ≤ arr.size) :
12+
kthElement arr k = arr[(k - 1).toNat]! :=
13+
sorry
14+
15+
end NumpySpec.DafnyBenchmarks.SynthesisTask101
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask142
2+
3+
/-- Count the number of positions where all three arrays have identical values -/
4+
def countIdenticalPositions (a b c : Array Int) : Nat :=
5+
sorry
6+
7+
/-- Specification: The count equals the number of indices where all three arrays have the same value -/
8+
theorem countIdenticalPositions_spec (a b c : Array Int)
9+
(h_len : a.size = b.size ∧ b.size = c.size) :
10+
let count := (List.range a.size).filter (fun i => a[i]! = b[i]! ∧ b[i]! = c[i]!)
11+
countIdenticalPositions a b c = count.length := by
12+
sorry
13+
14+
end NumpySpec.DafnyBenchmarks.SynthesisTask142
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask143
2+
3+
/-- Count the number of arrays in a list of arrays -/
4+
def countArrays (arrays : List (Array Int)) : Nat :=
5+
sorry
6+
7+
/-- Specification: The count equals the length of the list -/
8+
theorem countArrays_spec (arrays : List (Array Int)) :
9+
countArrays arrays = arrays.length := by
10+
sorry
11+
12+
end NumpySpec.DafnyBenchmarks.SynthesisTask143
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import Std.Do.Triple
2+
import Std.Tactic.Do
3+
4+
open Std.Do
5+
6+
/-- MaxDifference: Find the maximum difference between any two elements in an array.
7+
8+
Given an array with at least 2 elements, returns the maximum value of a[i] - a[j]
9+
for any valid indices i and j.
10+
11+
Example: maxDifference([1, 5, 3]) could return 4 (5 - 1)
12+
-/
13+
def maxDifference (a : Array Int) : Id Int :=
14+
sorry
15+
16+
/-- Specification: maxDifference returns a value that is greater than or equal to
17+
any difference between two elements in the array.
18+
19+
Precondition: The array has at least 2 elements
20+
Postcondition: For all pairs of indices i,j, the difference a[i] - a[j] is at most the result
21+
-/
22+
theorem maxDifference_spec (a : Array Int) :
23+
⦃⌜a.size > 1⌝⦄
24+
maxDifference a
25+
⦃⇓diff => ⌜∀ i j : Fin a.size, a[i] - a[j] ≤ diff⌝⦄ := by
26+
sorry
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
-- Synthesis Task 290: Find the longest list from a sequence of lists
2+
3+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask290
4+
5+
/-- Returns the longest list from a sequence of lists -/
6+
def maxLengthList (lists : List (List Int)) : List Int :=
7+
sorry
8+
9+
/-- Specification: The returned list is at least as long as any list in the input,
10+
and it is one of the lists in the input -/
11+
theorem maxLengthList_spec (lists : List (List Int)) (h : lists.length > 0) :
12+
let maxList := maxLengthList lists
13+
(∀ l ∈ lists, l.length ≤ maxList.length) ∧ maxList ∈ lists :=
14+
sorry
15+
16+
end NumpySpec.DafnyBenchmarks.SynthesisTask290
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Std.Do.Triple
2+
import Std.Tactic.Do
3+
4+
open Std.Do
5+
6+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask433
7+
8+
/-- Check if n is greater than all elements in array a -/
9+
def isGreater (n : Int) (a : Array Int) : Bool :=
10+
sorry
11+
12+
/-- Specification: If result is true, n is greater than all elements;
13+
if false, there exists at least one element >= n -/
14+
theorem isGreater_spec (n : Int) (a : Array Int) :
15+
(isGreater n a = true → ∀ i : Nat, i < a.size → n > a[i]!) ∧
16+
(isGreater n a = false → ∃ i : Nat, i < a.size ∧ n ≤ a[i]!) := by
17+
sorry
18+
19+
end NumpySpec.DafnyBenchmarks.SynthesisTask433
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import Std.Do.Triple
2+
import Std.Tactic.Do
3+
4+
open Std.Do
5+
6+
/-- Min: Find the minimum element in a non-empty list.
7+
8+
Recursively finds the minimum element by comparing the last element
9+
with the minimum of the prefix.
10+
-/
11+
def min : List Int → Int
12+
| [] => 0 -- Should not happen given precondition
13+
| [a] => a
14+
| l =>
15+
let minPrefix := min (l.take (l.length - 1))
16+
let lastElem := l.getLast!
17+
if lastElem ≤ minPrefix then lastElem else minPrefix
18+
termination_by l => l.length
19+
decreasing_by sorry
20+
21+
/-- Max: Find the maximum element in a non-empty list.
22+
23+
Recursively finds the maximum element by comparing the last element
24+
with the maximum of the prefix.
25+
-/
26+
def max : List Int → Int
27+
| [] => 0 -- Should not happen given precondition
28+
| [a] => a
29+
| l =>
30+
let maxPrefix := max (l.take (l.length - 1))
31+
let lastElem := l.getLast!
32+
if lastElem ≥ maxPrefix then lastElem else maxPrefix
33+
termination_by l => l.length
34+
decreasing_by sorry
35+
36+
/-- SumMinMax: Return the sum of the minimum and maximum elements in an array.
37+
38+
Given a non-empty array, returns the sum of its minimum and maximum elements.
39+
40+
Example: sumMinMax([3, 1, 4, 1, 5]) = 1 + 5 = 6
41+
-/
42+
def sumMinMax (a : Array Int) : Id Int :=
43+
sorry
44+
45+
/-- Specification: sumMinMax returns the sum of the minimum and maximum elements.
46+
47+
Precondition: The array is non-empty
48+
Postcondition: The result equals max(a.toList) + min(a.toList)
49+
-/
50+
theorem sumMinMax_spec (a : Array Int) :
51+
⦃⌜a.size > 0⌝⦄
52+
sumMinMax a
53+
⦃⇓sum => ⌜sum = max a.toList + min a.toList⌝⦄ := by
54+
sorry
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Std.Do.Triple
2+
import Std.Tactic.Do
3+
4+
open Std.Do
5+
6+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask566
7+
8+
/-- Helper function: power of 10 -/
9+
def power10 (n : Nat) : Nat :=
10+
sorry
11+
12+
/-- Helper function: number of digits in a natural number -/
13+
def numberOfDigits (n : Nat) : Nat :=
14+
sorry
15+
16+
/-- Helper function: sum of digits recursive implementation -/
17+
def sumDigitsRecursive (n : Nat) (p : Nat) : Nat :=
18+
sorry
19+
20+
/-- Helper function: sum of digits -/
21+
def sumDigits (n : Nat) : Nat :=
22+
sorry
23+
24+
/-- Compute the sum of digits of a natural number -/
25+
def sumOfDigits (number : Nat) : Nat :=
26+
sorry
27+
28+
/-- Specification: The sum of digits is non-negative and equals sumDigits -/
29+
theorem sumOfDigits_spec (number : Nat) :
30+
sumOfDigits number ≥ 0 ∧ sumOfDigits number = sumDigits number := by
31+
sorry
32+
33+
/-- Power of 10 is at least 1 -/
34+
theorem power10_ge_one (n : Nat) :
35+
power10 n ≥ 1 := by
36+
sorry
37+
38+
/-- Power of 10 for positive n is divisible by 10 -/
39+
theorem power10_div_ten (n : Nat) (h : n > 0) :
40+
power10 n % 10 = 0 := by
41+
sorry
42+
43+
/-- Number of digits is at least 1 -/
44+
theorem numberOfDigits_ge_one (n : Nat) :
45+
numberOfDigits n ≥ 1 := by
46+
sorry
47+
48+
/-- Single digit characterization -/
49+
theorem numberOfDigits_single (n : Nat) :
50+
numberOfDigits n = 10 ≤ n ∧ n ≤ 9 := by
51+
sorry
52+
53+
end NumpySpec.DafnyBenchmarks.SynthesisTask566
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
-- Synthesis Task 573: Product of unique elements
2+
3+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask573
4+
5+
/-- Calculate the product of unique elements in an array -/
6+
def uniqueProduct (arr : Array Int) : Int :=
7+
sorry
8+
9+
/-- Specification: Product includes each distinct element exactly once -/
10+
theorem uniqueProduct_spec (arr : Array Int) :
11+
∃ uniqueElems : List Int,
12+
(∀ x : Int, x ∈ uniqueElems ↔ ∃ i : Nat, i < arr.size ∧ arr[i]! = x) ∧
13+
(∀ i j : Nat, i < j → j < uniqueElems.length → uniqueElems[i]! ≠ uniqueElems[j]!) ∧
14+
uniqueProduct arr = uniqueElems.foldl (· * ·) 1 :=
15+
sorry
16+
17+
end NumpySpec.DafnyBenchmarks.SynthesisTask573
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Std.Do.Triple
2+
import Std.Tactic.Do
3+
4+
open Std.Do
5+
6+
namespace NumpySpec.DafnyBenchmarks.SynthesisTask579
7+
8+
/-- Predicate to check if x is in array a -/
9+
def inArray (a : Array Int) (x : Int) : Prop :=
10+
∃ i : Nat, i < a.size ∧ a[i]! = x
11+
12+
/-- Find elements that are in exactly one of the two arrays (symmetric difference) -/
13+
def dissimilarElements (a : Array Int) (b : Array Int) : List Int :=
14+
sorry
15+
16+
/-- Specification: All elements in result are in exactly one array (not both, not neither),
17+
and all elements in the result are distinct -/
18+
theorem dissimilarElements_spec (a : Array Int) (b : Array Int) :
19+
let result := dissimilarElements a b
20+
(∀ x ∈ result, (inArray a x) ≠ (inArray b x)) ∧
21+
(∀ i j : Nat, i < result.length → j < result.length → i < j → result[i]! ≠ result[j]!) := by
22+
sorry
23+
24+
end NumpySpec.DafnyBenchmarks.SynthesisTask579

0 commit comments

Comments
 (0)