File size: 5,839 Bytes
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*@ Import Lean
import Algorithms.minimal_representation.lean.helper_lib
open scoped SimpleC
*/
/*
 * Find the lexicographically smallest cyclic rotation of an integer sequence.
 *
 * The sequence is copied twice into b so that every cyclic rotation is
 * represented by one contiguous segment.  The two-candidate scan eliminates
 * at least one possible starting position after every mismatch, and therefore
 * runs in linear time.
 *
 * The verification case limits n to 1000.  The caller provides n initialized
 * elements in a, room for 2 * n elements in b, and room for n elements in out.
 * The function writes the smallest rotation to out and returns its zero-based
 * starting position in a.
 */
/*@ Extern Coq
      (MRRotation : list Z -> Z -> list Z)
      (MRRotationPrefixEq : list Z -> Z -> Z -> Z -> Prop)
      (MRRotationEq : list Z -> Z -> Z -> Prop)
      (MRFirstMinimalRotationAt : list Z -> Z -> Prop)
      (MRCandidateState : list Z -> Z -> Z -> Z -> Prop)
 */

int minimal_representation(int *a, int n, int *b, int *out)
/*@ With (l : list Z) (best : Z)
    Require
      1 <= n && n <= 1000 &&
      Zlength(l) == n &&
      0 <= best && best < n &&
      MRFirstMinimalRotationAt(l, best) &&
      IntArray::full(a, n, l) *
      IntArray::undef_full(b, 2 * n) *
      IntArray::undef_full(out, n)
    Ensure
      exists bl,
      __return == best &&
      MRFirstMinimalRotationAt(l, __return) &&
      IntArray::full(a, n, l) *
      IntArray::full(b, 2 * n, bl) *
      IntArray::full(out, n, MRRotation(l, __return))
 */
{
    int p = 0;

    /*@ Inv Assert
        a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
        1 <= n@pre && n@pre <= 1000 &&
        Zlength(l) == n@pre &&
        0 <= best && best < n@pre &&
        0 <= p && p <= n@pre &&
        MRFirstMinimalRotationAt(l, best) &&
        IntArray::full(a@pre, n@pre, l) *
        IntArray::seg(b@pre, 0, p, sublist(0, p, l)) *
        IntArray::undef_seg(b@pre, p, n@pre) *
        IntArray::seg(b@pre, n@pre, n@pre + p, sublist(0, p, l)) *
        IntArray::undef_seg(b@pre, n@pre + p, 2 * n@pre) *
        IntArray::undef_full(out@pre, n@pre)
     */
    while (p < n) {
        b[p] = a[p];
        b[n + p] = a[p];
        ++p;
    }

    /*@ Assert
        a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
        1 <= n@pre && n@pre <= 1000 &&
        Zlength(l) == n@pre &&
        p == n@pre &&
        0 <= best && best < n@pre &&
        MRFirstMinimalRotationAt(l, best) &&
        IntArray::full(a@pre, n@pre, l) *
        IntArray::full(b@pre, 2 * n@pre, app(l, l)) *
        IntArray::undef_full(out@pre, n@pre)
     */

    int i = 0;
    int j = 1;
    int k = 0;

    /*@ Inv Assert
        a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
        1 <= n@pre && n@pre <= 1000 &&
        Zlength(l) == n@pre &&
        p == n@pre &&
        0 <= best && best < n@pre &&
        0 <= i && i < 2 * n@pre &&
        0 <= j && j < 2 * n@pre &&
        i != j &&
        (i < n@pre || j < n@pre) &&
        0 <= k && k < n@pre &&
        MRFirstMinimalRotationAt(l, best) &&
        MRCandidateState(l, best, i, j) &&
        IntArray::full(a@pre, n@pre, l) *
        IntArray::full(b@pre, 2 * n@pre, app(l, l)) *
        IntArray::undef_full(out@pre, n@pre)
     */
    while (i < n && j < n) {
        k = 0;

        /*@ Inv Assert
            a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
            1 <= n@pre && n@pre <= 1000 &&
            Zlength(l) == n@pre &&
            p == n@pre &&
            0 <= best && best < n@pre &&
            0 <= i && i < n@pre &&
            0 <= j && j < n@pre &&
            i != j &&
            0 <= k && k <= n@pre &&
            MRFirstMinimalRotationAt(l, best) &&
            MRCandidateState(l, best, i, j) &&
            MRRotationPrefixEq(l, i, j, k) &&
            IntArray::full(a@pre, n@pre, l) *
            IntArray::full(b@pre, 2 * n@pre, app(l, l)) *
            IntArray::undef_full(out@pre, n@pre)
         */
        while (k < n && b[i + k] == b[j + k]) {
            ++k;
        }

        if (k == n) {
            break;
        }

        if (b[i + k] > b[j + k]) {
            i = i + k + 1;
            if (i == j) {
                ++i;
            }
        }
        else {
            j = j + k + 1;
            if (i == j) {
                ++j;
            }
        }
    }

    /*@ Assert
        a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
        1 <= n@pre && n@pre <= 1000 &&
        Zlength(l) == n@pre &&
        p == n@pre &&
        0 <= best && best < n@pre &&
        0 <= i && i < 2 * n@pre &&
        0 <= j && j < 2 * n@pre &&
        i != j &&
        (i < n@pre || j < n@pre) &&
        0 <= k && k <= n@pre &&
        (i < j => i == best) &&
        (i >= j => j == best) &&
        MRFirstMinimalRotationAt(l, best) &&
        IntArray::full(a@pre, n@pre, l) *
        IntArray::full(b@pre, 2 * n@pre, app(l, l)) *
        IntArray::undef_full(out@pre, n@pre)
     */
    if (i < j) {
        p = i;
    }
    else {
        p = j;
    }

    k = 0;
    /*@ Inv Assert
        a == a@pre && n == n@pre && b == b@pre && out == out@pre &&
        1 <= n@pre && n@pre <= 1000 &&
        Zlength(l) == n@pre &&
        0 <= best && best < n@pre &&
        p == best &&
        0 <= p && p < n@pre &&
        0 <= i && i < 2 * n@pre &&
        0 <= j && j < 2 * n@pre &&
        i != j &&
        0 <= k && k <= n@pre &&
        MRFirstMinimalRotationAt(l, best) &&
        IntArray::full(a@pre, n@pre, l) *
        IntArray::full(b@pre, 2 * n@pre, app(l, l)) *
        IntArray::seg(out@pre, 0, k, sublist(0, k, MRRotation(l, p))) *
        IntArray::undef_seg(out@pre, k, n@pre)
     */
    while (k < n) {
        out[k] = b[p + k];
        ++k;
    }

    return p;
}