File size: 3,200 Bytes
3805fe5
 
 
 
bff4ec7
3805fe5
 
 
bff4ec7
 
 
 
 
 
 
 
3805fe5
bff4ec7
 
 
 
3805fe5
 
bff4ec7
 
3805fe5
bff4ec7
 
3805fe5
 
bff4ec7
 
 
3805fe5
bff4ec7
 
 
 
3805fe5
 
bff4ec7
 
 
 
3805fe5
bff4ec7
 
 
 
 
 
3805fe5
bff4ec7
 
 
 
 
 
3805fe5
 
bff4ec7
 
 
3805fe5
bff4ec7
 
 
 
 
 
 
 
 
 
 
 
3805fe5
bff4ec7
 
 
 
 
3805fe5
 
bff4ec7
 
 
3805fe5
bff4ec7
 
 
 
3805fe5
bff4ec7
 
3805fe5
bff4ec7
3805fe5
bff4ec7
 
3805fe5
bff4ec7
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105




/*@ Extern Coq
      (Forall : {A} -> (A -> Prop) -> list A -> Prop)
      (Z::le : Z -> Z -> Prop)
      (Z::ge : Z -> Z -> Prop)
      (LISLength : list Z -> Z -> Prop)
      (LISDPTablePrefix : list Z -> list Z -> Z -> Prop)
      (LISInnerProgress : list Z -> list Z -> Z -> Z -> Prop)
      (LISBestSoFar : list Z -> Z -> Z -> Prop)
 */
/*@ Import Coq Require Import PVbench.Algorithms.longest_increasing_subsequence.rocq.spec_lib */
/*@ Import Coq Require Import PVbench.Algorithms.longest_increasing_subsequence.rocq.helper_lib */

int lengthOfLIS(int *nums, int numsSize)
/*@ With (l : list Z)
    Require
      1 <= numsSize && numsSize <= 100000 &&
      Zlength(l) == numsSize &&
      Forall(Z::le(-10000), l) && Forall(Z::ge(10000), l) &&
      IntArray::full(nums, numsSize, l)
    Ensure
      LISLength(l, __return) &&
      IntArray::full(nums, numsSize, l)
 */
{
  int dp[100000];

  int ans = 1;
  /*@ Inv Assert
      exists d,
      nums == nums@pre && numsSize == numsSize@pre &&
      1 <= numsSize@pre && numsSize@pre <= 100000 &&
      Zlength(l) == numsSize@pre &&
      0 <= i && i <= numsSize@pre &&
      1 <= ans && ans <= numsSize@pre &&
      Zlength(d) == i &&
      (forall (k : Z), (0 <= k && k < i) => (1 <= d[k] && d[k] <= k + 1)) &&
      LISDPTablePrefix(l, d, i) &&
      LISBestSoFar(l, i, ans) &&
      IntArray::full(nums, numsSize@pre, l) *
      IntArray::seg(dp, 0, i, d) *
      IntArray::undef_seg(dp, i, 100000)
   */
  for (int i = 0; i < numsSize; ++i) {
    dp[i] = 1;

    /*@ Inv Assert
        exists d,
        nums == nums@pre && numsSize == numsSize@pre &&
        1 <= numsSize@pre && numsSize@pre <= 100000 &&
        Zlength(l) == numsSize@pre &&
        0 <= i && i < numsSize@pre &&
        0 <= j && j <= i &&
        1 <= ans && ans <= numsSize@pre &&
        LISBestSoFar(l, i, ans) &&
        Zlength(d) == i + 1 &&
        (forall (k : Z), (0 <= k && k < i + 1) => (1 <= d[k] && d[k] <= k + 1)) &&
        LISInnerProgress(l, d, i, j) &&
        IntArray::full(nums, numsSize@pre, l) *
        IntArray::seg(dp, 0, i + 1, d) *
        IntArray::undef_seg(dp, i + 1, 100000)
     */
    for (int j = 0; j < i; ++j) {
      if (nums[j] < nums[i]) {
        int candidate = dp[j] + 1;
        if (candidate > dp[i]) {
          dp[i] = candidate;
        }
      }
    }

    /*@ Assert
        exists d,
        nums == nums@pre && numsSize == numsSize@pre &&
        1 <= numsSize@pre && numsSize@pre <= 100000 &&
        Zlength(l) == numsSize@pre &&
        0 <= i && i < numsSize@pre &&
        1 <= ans && ans <= numsSize@pre &&
        LISBestSoFar(l, i, ans) &&
        Zlength(d) == i + 1 &&
        (forall (k : Z), (0 <= k && k < i + 1) => (1 <= d[k] && d[k] <= k + 1)) &&
        LISDPTablePrefix(l, d, i + 1) &&
        IntArray::full(nums, numsSize@pre, l) *
        IntArray::seg(dp, 0, i + 1, d) *
        IntArray::undef_seg(dp, i + 1, 100000)
     */
    if (dp[i] > ans) {
      ans = dp[i];
    }
    
  }

  
  /*@ Assert
      nums == nums@pre && numsSize == numsSize@pre &&
      LISLength(l, ans) &&
      IntArray::full(nums, numsSize@pre, l) *
      IntArray::undef_full(dp, 100000)
   */
  return ans;
}