3 (** {1 Theory of maps} *)
5 (** {2 Generic Maps} *)
9 type map 'a 'b = 'a -> 'b
11 let function get (f: map 'a 'b) (x: 'a) : 'b = f x
13 let ghost function set (f: map 'a 'b) (x: 'a) (v: 'b) : map 'a 'b =
14 fun y -> if pure {y = x} then v else f y
16 (** syntactic sugar *)
17 let function ([]) (f: map 'a 'b) (x: 'a) : 'b = f x
18 let ghost function ([<-]) (f: map 'a 'b) (x: 'a) (v: 'b) : map 'a 'b = set f x v
26 let function const (v: 'b) : map 'a 'b = fun _ -> v
32 predicate (==) (m1 m2: 'a -> 'b) = forall x: 'a. m1 x = m2 x
35 forall m1 m2: 'a -> 'b. m1 == m2 -> m1 = m2
36 (* This lemma is actually provable in Why3, because of how
37 eliminate_epsilon handles equality to a lambda-term. *)
41 (** {2 Sorted Maps (indexed by integers)} *)
52 predicate sorted_sub (a : map int elt) (l u : int) =
53 forall i1 i2 : int. l <= i1 <= i2 < u -> le a[i1] a[i2]
54 (** `sorted_sub a l u` is true whenever the array segment `a(l..u-1)`
55 is sorted w.r.t order relation `le` *)
59 (** {2 Maps Equality (indexed by integers)} *)
66 predicate map_eq_sub (a1 a2 : map int 'a) (l u : int) =
67 forall i:int. l <= i < u -> a1[i] = a2[i]
76 predicate exchange (a1 a2: map int 'a) (l u i j: int) =
77 l <= i < u /\ l <= j < u /\
78 a1[i] = a2[j] /\ a1[j] = a2[i] /\
79 (forall k:int. l <= k < u -> k <> i -> k <> j -> a1[k] = a2[k])
82 forall a: map int 'a, l u i j: int.
83 l <= i < u -> l <= j < u ->
84 exchange a a[i <- a[j]][j <- a[i]] l u i j
88 (** {2 Sum of elements of a map (indexed by integers)} *)
96 (** `sum m l h` is the sum of `m[i]` for `l <= i < h` *)
97 function sum (m: map int int) (l h: int) : int = S.sum (get m) l h
101 (** {2 Number of occurrences} *)
103 (* TODO: we could define Occ using theory int.NumOf *)
109 function occ (v: 'a) (m: map int 'a) (l u: int) : int
110 (** number of occurrences of `v` in `m` between `l` included and `u` excluded *)
113 forall v: 'a, m: map int 'a, l u: int.
114 u <= l -> occ v m l u = 0
116 axiom occ_right_no_add:
117 forall v: 'a, m: map int 'a, l u: int.
118 l < u -> m[u-1] <> v -> occ v m l u = occ v m l (u-1)
121 forall v: 'a, m: map int 'a, l u: int.
122 l < u -> m[u-1] = v -> occ v m l u = 1 + occ v m l (u-1)
124 lemma occ_left_no_add:
125 forall v: 'a, m: map int 'a, l u: int.
126 l < u -> m[l] <> v -> occ v m l u = occ v m (l+1) u
129 forall v: 'a, m: map int 'a, l u: int.
130 l < u -> m[l] = v -> occ v m l u = 1 + occ v m (l+1) u
133 forall v: 'a, m: map int 'a, l u: int.
134 l <= u -> 0 <= occ v m l u <= u - l
135 (* direct when l>=u, by induction on u when l <= u *)
138 forall v: 'a, m: map int 'a, l mid u: int.
139 l <= mid <= u -> occ v m l u = occ v m l mid + occ v m mid u
140 (* by induction on u *)
143 forall v: 'a, m: map int 'a, l u: int.
144 (forall i: int. l <= i < u -> m[i] <> v) -> occ v m l u = 0
145 (* by induction on u *)
148 forall v: 'a, m: map int 'a, l u: int.
149 occ v m l u > 0 -> exists i: int. l <= i < u /\ m[i] = v
152 forall m: map int 'a, l u i: int.
153 l <= i < u -> occ m[i] m l u > 0
156 forall v: 'a, m1 m2: map int 'a, l u: int.
157 (forall i: int. l <= i < u -> m1[i] = m2[i]) -> occ v m1 l u = occ v m2 l u
158 (* by induction on u *)
161 forall m: map int 'a, l u i j: int, x y z: 'a.
162 l <= i < u -> l <= j < u -> i <> j ->
163 occ z m[i <- x][j <- y] l u =
164 occ z m[i <- y][j <- x] l u
174 predicate permut (m1 m2: map int 'a) (l u: int) =
175 forall v: 'a. occ v m1 l u = occ v m2 l u
177 lemma permut_trans: (* provable, yet useful *)
178 forall a1 a2 a3 : map int 'a. forall l u : int.
179 permut a1 a2 l u -> permut a2 a3 l u -> permut a1 a3 l u
181 lemma permut_exists :
182 forall a1 a2: map int 'a, l u i: int.
183 permut a1 a2 l u -> l <= i < u ->
184 exists j: int. l <= j < u /\ a1[j] = a2[i]
189 (** {2 Injectivity and surjectivity for maps (indexed by integers)} *)
196 predicate injective (a: map int int) (n: int) =
197 forall i j: int. 0 <= i < n -> 0 <= j < n -> i <> j -> a[i] <> a[j]
198 (** `injective a n` is true when `a` is an injection
199 on the domain `(0..n-1)` *)
201 predicate surjective (a: map int int) (n: int) =
202 forall i: int. 0 <= i < n -> exists j: int. (0 <= j < n /\ a[j] = i)
203 (** `surjective a n` is true when `a` is a surjection
204 from `(0..n-1)` to `(0..n-1)` *)
206 predicate range (a: map int int) (n: int) =
207 forall i: int. 0 <= i < n -> 0 <= a[i] < n
208 (** `range a n` is true when `a` maps the domain
209 `(0..n-1)` into `(0..n-1)` *)
211 lemma injective_surjective:
212 forall a: map int int, n: int.
213 injective a n -> range a n -> surjective a n
214 (** main lemma: an injection on `(0..n-1)` that
215 ranges into `(0..n-1)` is also a surjection *)
220 forall m: map int int, n: int.
221 injective m n <-> forall v:int. (occ v m 0 n <= 1)