From Stdlib Require Import Strings.String.
From Stdlib Require Import String Ascii.
From Stdlib Require Arith.
From stdpp Require Import base.
From stdpp Require Import fin_maps.
From stdpp Require Import gmap.
From stdpp Require Import base gmultiset.
From Stdlib Require Classical.
From Stdlib Require Import ZArith.
From stdpp.bitvector Require Import definitions tactics.
From Stdlib Require Import Sorting.Sorted.
From Stdlib Require Import Reals.Rbasic_fun.
From Stdlib Require Import Reals.Abstract.ConstructiveAbs.
From Stdlib Require Import Reals.Rdefinitions.
From stdpp Require Import list_relations.
From stdpp Require Import list_numbers.
From stdpp Require Import functions.
From Stdlib Require Import ClassicalEpsilon.
From stdpp Require Import base decidable.
From Stdlib Require Import ZArith.Zeuclid.
From Stdlib Require Import ZArith.Znumtheory.
From stdpp Require Import propset.
From Stdlib Require Import Reals.
Require Import Why3.Base.
Require Import Why3.why3.Ref.Ref.
Require Import Why3.pigeon.Pigeonhole.
Open Scope Z_scope.
Definition mem (x : Z) (a : list Z) := ∃(i : Z), (0%Z ≤ i ∧ i < Z.of_nat (length a)) ∧ nth (Z.to_nat i) a inhabitant = x.
Axiom identity : Z -> Z.
Axiom identity_def : forall  (y : Z), identity y = y.
Theorem mex'vc (a : list Z) : let n : Z := Z.of_nat (length a) in 0%Z ≤ n ∧ (∀(used : list bool), (∀(i : Z), 0%Z ≤ i ∧ i < n -> nth (Z.to_nat i) used inhabitant = false) ∧ Z.of_nat (length used) = n -> (let o1 : Z -> Z := identity in let o2 : Z := n - 1%Z in (0%Z ≤ o2 + 1%Z -> ((∀(x : Z), 0%Z ≤ x ∧ x < n -> nth (Z.to_nat x) used inhabitant = true -> mem x a ∧ (0%Z ≤ o1 x ∧ o1 x < n) ∧ nth (Z.to_nat (o1 x)) a inhabitant = x) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < 0%Z -> 0%Z ≤ nth (Z.to_nat j) a inhabitant ∧ nth (Z.to_nat j) a inhabitant < n -> nth (Z.to_nat (nth (Z.to_nat j) a inhabitant)) used inhabitant = true ∧ (0%Z ≤ o1 (nth (Z.to_nat j) a inhabitant) ∧ o1 (nth (Z.to_nat j) a inhabitant) < n) ∧ nth (Z.to_nat (o1 (nth (Z.to_nat j) a inhabitant))) a inhabitant = nth (Z.to_nat j) a inhabitant)) ∧ (∀(idx : Z -> Z) (used1 : list bool), length used1 = length used -> (∀(i : Z), (0%Z ≤ i ∧ i ≤ o2) ∧ (∀(x : Z), 0%Z ≤ x ∧ x < n -> nth (Z.to_nat x) used1 inhabitant = true -> mem x a ∧ (0%Z ≤ idx x ∧ idx x < n) ∧ nth (Z.to_nat (idx x)) a inhabitant = x) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < i -> 0%Z ≤ nth (Z.to_nat j) a inhabitant ∧ nth (Z.to_nat j) a inhabitant < n -> nth (Z.to_nat (nth (Z.to_nat j) a inhabitant)) used1 inhabitant = true ∧ (0%Z ≤ idx (nth (Z.to_nat j) a inhabitant) ∧ idx (nth (Z.to_nat j) a inhabitant) < n) ∧ nth (Z.to_nat (idx (nth (Z.to_nat j) a inhabitant))) a inhabitant = nth (Z.to_nat j) a inhabitant) -> (0%Z ≤ i ∧ i < Z.of_nat (length a)) ∧ (let x : Z := nth (Z.to_nat i) a inhabitant in ∀(o3 : bool), (if decide (0%Z ≤ x) then o3 = (if decide (x < n) then true else false) else o3 = false) -> (if decide (o3 = true) then let o4 : bool := true in (0%Z ≤ x ∧ x < Z.of_nat (length used1)) ∧ (length (set_list used1 (Z.to_nat x) o4) = length used1 -> nth_i (set_list used1 (Z.to_nat x) o4) = fun_updt (nth_i used1) x o4 -> (∀(x1 : Z), 0%Z ≤ x1 ∧ x1 < n -> nth (Z.to_nat x1) (set_list used1 (Z.to_nat x) o4) inhabitant = true -> mem x1 a ∧ (0%Z ≤ fun_updt idx x i x1 ∧ fun_updt idx x i x1 < n) ∧ nth (Z.to_nat (fun_updt idx x i x1)) a inhabitant = x1) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < i + 1%Z -> 0%Z ≤ nth (Z.to_nat j) a inhabitant ∧ nth (Z.to_nat j) a inhabitant < n -> nth (Z.to_nat (nth (Z.to_nat j) a inhabitant)) (set_list used1 (Z.to_nat x) o4) inhabitant = true ∧ (0%Z ≤ fun_updt idx x i (nth (Z.to_nat j) a inhabitant) ∧ fun_updt idx x i (nth (Z.to_nat j) a inhabitant) < n) ∧ nth (Z.to_nat (fun_updt idx x i (nth (Z.to_nat j) a inhabitant))) a inhabitant = nth (Z.to_nat j) a inhabitant)) else (∀(x1 : Z), 0%Z ≤ x1 ∧ x1 < n -> nth (Z.to_nat x1) used1 inhabitant = true -> mem x1 a ∧ (0%Z ≤ idx x1 ∧ idx x1 < n) ∧ nth (Z.to_nat (idx x1)) a inhabitant = x1) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < i + 1%Z -> 0%Z ≤ nth (Z.to_nat j) a inhabitant ∧ nth (Z.to_nat j) a inhabitant < n -> nth (Z.to_nat (nth (Z.to_nat j) a inhabitant)) used1 inhabitant = true ∧ (0%Z ≤ idx (nth (Z.to_nat j) a inhabitant) ∧ idx (nth (Z.to_nat j) a inhabitant) < n) ∧ nth (Z.to_nat (idx (nth (Z.to_nat j) a inhabitant))) a inhabitant = nth (Z.to_nat j) a inhabitant)))) ∧ ((∀(x : Z), 0%Z ≤ x ∧ x < n -> nth (Z.to_nat x) used1 inhabitant = true -> mem x a ∧ (0%Z ≤ idx x ∧ idx x < n) ∧ nth (Z.to_nat (idx x)) a inhabitant = x) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < o2 + 1%Z -> 0%Z ≤ nth (Z.to_nat j) a inhabitant ∧ nth (Z.to_nat j) a inhabitant < n -> nth (Z.to_nat (nth (Z.to_nat j) a inhabitant)) used1 inhabitant = true ∧ (0%Z ≤ idx (nth (Z.to_nat j) a inhabitant) ∧ idx (nth (Z.to_nat j) a inhabitant) < n) ∧ nth (Z.to_nat (idx (nth (Z.to_nat j) a inhabitant))) a inhabitant = nth (Z.to_nat j) a inhabitant) -> ((0%Z ≤ 0%Z ∧ 0%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < 0%Z -> nth (Z.to_nat j) used1 inhabitant = true ∧ 0%Z ≤ idx j ∧ idx j < n) ∧ (if decide (0%Z ≤ - 1%Z) then (0%Z ≤ - 1%Z ∧ - 1%Z < n) ∧ nth (Z.to_nat (- 1%Z)) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < 0%Z -> ¬ nth (Z.to_nat j) a inhabitant = n)) ∧ (∀(posn : Z) (r : Z), (0%Z ≤ r ∧ r ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r -> nth (Z.to_nat j) used1 inhabitant = true ∧ 0%Z ≤ idx j ∧ idx j < n) ∧ (if decide (0%Z ≤ posn) then (0%Z ≤ posn ∧ posn < n) ∧ nth (Z.to_nat posn) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r -> ¬ nth (Z.to_nat j) a inhabitant = n) -> (r < n -> 0%Z ≤ r ∧ r < Z.of_nat (length used1)) ∧ (∀(o3 : bool), (if decide (r < n) then o3 = nth (Z.to_nat r) used1 inhabitant else o3 = false) -> (if decide (o3 = true) then (0%Z ≤ r ∧ r < Z.of_nat (length a)) ∧ (if decide (nth (Z.to_nat r) a inhabitant = n) then (0%Z ≤ n - r ∧ n - (r + 1%Z) < n - r) ∧ (0%Z ≤ r + 1%Z ∧ r + 1%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> nth (Z.to_nat j) used1 inhabitant = true ∧ 0%Z ≤ idx j ∧ idx j < n) ∧ (if decide (0%Z ≤ r) then (0%Z ≤ r ∧ r < n) ∧ nth (Z.to_nat r) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> ¬ nth (Z.to_nat j) a inhabitant = n) else (0%Z ≤ n - r ∧ n - (r + 1%Z) < n - r) ∧ (0%Z ≤ r + 1%Z ∧ r + 1%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> nth (Z.to_nat j) used1 inhabitant = true ∧ 0%Z ≤ idx j ∧ idx j < n) ∧ (if decide (0%Z ≤ posn) then (0%Z ≤ posn ∧ posn < n) ∧ nth (Z.to_nat posn) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> ¬ nth (Z.to_nat j) a inhabitant = n)) else ∀(o4 : bool), (if decide (r = n) then o4 = (if decide (0%Z ≤ posn) then true else false) else o4 = false) -> (o4 = true -> (let o5 : Z -> Z := fun_updt idx n posn in let o6 : Z := n + 1%Z in (0%Z ≤ n ∧ n < o6) ∧ (∀(i : Z), 0%Z ≤ i ∧ i < o6 -> 0%Z ≤ o5 i ∧ o5 i < n))) ∧ ((o4 = true -> (let o5 : Z -> Z := fun_updt idx n posn in ∃(i1 : Z) (i2 : Z), (0%Z ≤ i1 ∧ i1 < i2 ∧ i2 < n + 1%Z) ∧ o5 i1 = o5 i2)) -> (0%Z ≤ r ∧ r ≤ Z.of_nat (length a)) ∧ ¬ mem r a ∧ (∀(x : Z), 0%Z ≤ x ∧ x < r -> mem x a)))))))) ∧ (o2 + 1%Z < 0%Z -> ((0%Z ≤ 0%Z ∧ 0%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < 0%Z -> nth (Z.to_nat j) used inhabitant = true ∧ 0%Z ≤ o1 j ∧ o1 j < n) ∧ (if decide (0%Z ≤ - 1%Z) then (0%Z ≤ - 1%Z ∧ - 1%Z < n) ∧ nth (Z.to_nat (- 1%Z)) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < 0%Z -> ¬ nth (Z.to_nat j) a inhabitant = n)) ∧ (∀(posn : Z) (r : Z), (0%Z ≤ r ∧ r ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r -> nth (Z.to_nat j) used inhabitant = true ∧ 0%Z ≤ o1 j ∧ o1 j < n) ∧ (if decide (0%Z ≤ posn) then (0%Z ≤ posn ∧ posn < n) ∧ nth (Z.to_nat posn) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r -> ¬ nth (Z.to_nat j) a inhabitant = n) -> (r < n -> 0%Z ≤ r ∧ r < Z.of_nat (length used)) ∧ (∀(o3 : bool), (if decide (r < n) then o3 = nth (Z.to_nat r) used inhabitant else o3 = false) -> (if decide (o3 = true) then (0%Z ≤ r ∧ r < Z.of_nat (length a)) ∧ (if decide (nth (Z.to_nat r) a inhabitant = n) then (0%Z ≤ n - r ∧ n - (r + 1%Z) < n - r) ∧ (0%Z ≤ r + 1%Z ∧ r + 1%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> nth (Z.to_nat j) used inhabitant = true ∧ 0%Z ≤ o1 j ∧ o1 j < n) ∧ (if decide (0%Z ≤ r) then (0%Z ≤ r ∧ r < n) ∧ nth (Z.to_nat r) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> ¬ nth (Z.to_nat j) a inhabitant = n) else (0%Z ≤ n - r ∧ n - (r + 1%Z) < n - r) ∧ (0%Z ≤ r + 1%Z ∧ r + 1%Z ≤ n) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> nth (Z.to_nat j) used inhabitant = true ∧ 0%Z ≤ o1 j ∧ o1 j < n) ∧ (if decide (0%Z ≤ posn) then (0%Z ≤ posn ∧ posn < n) ∧ nth (Z.to_nat posn) a inhabitant = n else ∀(j : Z), 0%Z ≤ j ∧ j < r + 1%Z -> ¬ nth (Z.to_nat j) a inhabitant = n)) else ∀(o4 : bool), (if decide (r = n) then o4 = (if decide (0%Z ≤ posn) then true else false) else o4 = false) -> (o4 = true -> (let o5 : Z -> Z := fun_updt o1 n posn in let o6 : Z := n + 1%Z in (0%Z ≤ n ∧ n < o6) ∧ (∀(i : Z), 0%Z ≤ i ∧ i < o6 -> 0%Z ≤ o5 i ∧ o5 i < n))) ∧ ((o4 = true -> (let o5 : Z -> Z := fun_updt o1 n posn in ∃(i1 : Z) (i2 : Z), (0%Z ≤ i1 ∧ i1 < i2 ∧ i2 < n + 1%Z) ∧ o5 i1 = o5 i2)) -> (0%Z ≤ r ∧ r ≤ Z.of_nat (length a)) ∧ ¬ mem r a ∧ (∀(x : Z), 0%Z ≤ x ∧ x < r -> mem x a)))))))).
Admitted.
