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.int.NumOf.
Open Scope Z_scope.
Definition prefix_tl (x : Z) : Z := - x - 1%Z.
Axiom fc : (Z -> Z) -> Z -> bool.
Axiom fc'def : forall  (m : Z -> Z) (n : Z), (fc m n = true) = (0%Z ≤ m n).
Definition numof (m : Z -> Z) (l : Z) (r : Z) : Z := NumOf.numof (fc m) l r.
Definition is_permutation (a : list Z) := ∀(i : Z), 0%Z ≤ i ∧ i < Z.of_nat (length a) -> (0%Z ≤ nth (Z.to_nat i) a inhabitant ∧ nth (Z.to_nat i) a inhabitant < Z.of_nat (length a)) ∧ (∀(j : Z), 0%Z ≤ j ∧ j < Z.of_nat (length a) -> ¬ i = j -> ¬ nth (Z.to_nat i) a inhabitant = nth (Z.to_nat j) a inhabitant).
Definition loopinvariant (olda : list Z) (a : list Z) (m : Z) (m' : Z) (n : Z) := (∀(e : Z), 0%Z ≤ e ∧ e < n -> - n ≤ nth (Z.to_nat e) a inhabitant ∧ nth (Z.to_nat e) a inhabitant < n) ∧ (∀(e : Z), m < e ∧ e < n -> 0%Z ≤ nth (Z.to_nat e) a inhabitant) ∧ (∀(e : Z), m < e ∧ e < n -> nth (Z.to_nat (nth (Z.to_nat e) a inhabitant)) olda inhabitant = e) ∧ (∀(e : Z), 0%Z ≤ e ∧ e ≤ m' -> 0%Z ≤ nth (Z.to_nat e) a inhabitant -> nth (Z.to_nat e) olda inhabitant = nth (Z.to_nat e) a inhabitant) ∧ (∀(e : Z), 0%Z ≤ e ∧ e ≤ m -> nth (Z.to_nat e) a inhabitant ≤ m) ∧ (∀(e : Z), 0%Z ≤ e ∧ e ≤ m' -> nth (Z.to_nat e) a inhabitant < 0%Z -> nth (Z.to_nat (prefix_tl (nth (Z.to_nat e) a inhabitant))) olda inhabitant = e ∧ (prefix_tl (nth (Z.to_nat e) a inhabitant) ≤ m -> nth (Z.to_nat (prefix_tl (nth (Z.to_nat e) a inhabitant))) a inhabitant < 0%Z)).
Theorem inverse_in_place'vc (a : list Z) (fact0 : is_permutation a) : let n : Z := Z.of_nat (length a) in let o1 : Z := n - 1%Z in (0%Z - 1%Z ≤ o1 -> loopinvariant a a o1 o1 n ∧ (∀(a1 : list Z), length a1 = length a -> (∀(m : Z), (m ≤ o1 ∧ 0%Z ≤ m) ∧ loopinvariant a a1 m m n -> (0%Z ≤ m ∧ m < Z.of_nat (length a1)) ∧ (let o2 : Z := nth (Z.to_nat m) a1 inhabitant in if decide (0%Z ≤ o2) then (0%Z ≤ m ∧ m < Z.of_nat (length a1)) ∧ (length (set_list a1 (Z.to_nat m) (- 1%Z)) = length a1 -> nth_i (set_list a1 (Z.to_nat m) (- 1%Z)) = fun_updt (nth_i a1) m (- 1%Z) -> (let o3 : Z := prefix_tl m in (0%Z ≤ o2 ∧ o2 < Z.of_nat (length (set_list a1 (Z.to_nat m) (- 1%Z)))) ∧ ((nth (Z.to_nat o2) (set_list a1 (Z.to_nat m) (- 1%Z)) inhabitant ≤ m ∧ (0%Z ≤ o2 ∧ o2 ≤ m) ∧ (prefix_tl m ≤ o3 ∧ o3 < 0%Z) ∧ nth (Z.to_nat (prefix_tl o3)) a inhabitant = o2 ∧ nth (Z.to_nat (prefix_tl o3)) (set_list a1 (Z.to_nat m) (- 1%Z)) inhabitant < 0%Z ∧ nth (Z.to_nat m) (set_list a1 (Z.to_nat m) (- 1%Z)) inhabitant < 0%Z) ∧ (∀(e : Z), 0%Z ≤ e ∧ e < m -> nth (Z.to_nat e) (set_list a1 (Z.to_nat m) (- 1%Z)) inhabitant < 0%Z -> ¬ nth (Z.to_nat e) (set_list a1 (Z.to_nat m) (- 1%Z)) inhabitant = o3) ∧ loopinvariant a (set_list a1 (Z.to_nat m) (- 1%Z)) m (m - 1%Z) n) ∧ (∀(j : Z) (a2 : list Z), length a2 = length (set_list a1 (Z.to_nat m) (- 1%Z)) -> (nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant ≤ m ∧ (0%Z ≤ nth (Z.to_nat (prefix_tl j)) a inhabitant ∧ nth (Z.to_nat (prefix_tl j)) a inhabitant ≤ m) ∧ (prefix_tl m ≤ j ∧ j < 0%Z) ∧ nth (Z.to_nat (prefix_tl j)) a2 inhabitant < 0%Z ∧ nth (Z.to_nat m) a2 inhabitant < 0%Z) ∧ (∀(e : Z), 0%Z ≤ e ∧ e < m -> nth (Z.to_nat e) a2 inhabitant < 0%Z -> ¬ nth (Z.to_nat e) a2 inhabitant = j) ∧ loopinvariant a a2 m (m - 1%Z) n -> (if decide (0%Z ≤ nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant) then (0%Z ≤ nth (Z.to_nat (prefix_tl j)) a inhabitant ∧ nth (Z.to_nat (prefix_tl j)) a inhabitant < Z.of_nat (length a2)) ∧ (length (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) = length a2 -> nth_i (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) = fun_updt (nth_i a2) (nth (Z.to_nat (prefix_tl j)) a inhabitant) j -> (0%Z ≤ nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant ∧ nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant < Z.of_nat (length (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j))) ∧ (0%Z ≤ numof (nth_i a2) 0%Z n ∧ numof (nth_i (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j)) 0%Z n < numof (nth_i a2) 0%Z n) ∧ (nth (Z.to_nat (nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant)) (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) inhabitant ≤ m ∧ (0%Z ≤ nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant ∧ nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant ≤ m) ∧ (prefix_tl m ≤ prefix_tl (nth (Z.to_nat (prefix_tl j)) a inhabitant) ∧ prefix_tl (nth (Z.to_nat (prefix_tl j)) a inhabitant) < 0%Z) ∧ nth (Z.to_nat (prefix_tl (prefix_tl (nth (Z.to_nat (prefix_tl j)) a inhabitant)))) a inhabitant = nth (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) a2 inhabitant ∧ nth (Z.to_nat (prefix_tl (prefix_tl (nth (Z.to_nat (prefix_tl j)) a inhabitant)))) (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) inhabitant < 0%Z ∧ nth (Z.to_nat m) (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) inhabitant < 0%Z) ∧ (∀(e : Z), 0%Z ≤ e ∧ e < m -> nth (Z.to_nat e) (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) inhabitant < 0%Z -> ¬ nth (Z.to_nat e) (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) inhabitant = prefix_tl (nth (Z.to_nat (prefix_tl j)) a inhabitant)) ∧ loopinvariant a (set_list a2 (Z.to_nat (nth (Z.to_nat (prefix_tl j)) a inhabitant)) j) m (m - 1%Z) n) else let o4 : Z := prefix_tl j in (0%Z ≤ m ∧ m < Z.of_nat (length a2)) ∧ (length (set_list a2 (Z.to_nat m) o4) = length a2 -> nth_i (set_list a2 (Z.to_nat m) o4) = fun_updt (nth_i a2) m o4 -> loopinvariant a (set_list a2 (Z.to_nat m) o4) (m - 1%Z) (m - 1%Z) n))))) else let o3 : Z := prefix_tl o2 in (0%Z ≤ m ∧ m < Z.of_nat (length a1)) ∧ (length (set_list a1 (Z.to_nat m) o3) = length a1 -> nth_i (set_list a1 (Z.to_nat m) o3) = fun_updt (nth_i a1) m o3 -> loopinvariant a (set_list a1 (Z.to_nat m) o3) (m - 1%Z) (m - 1%Z) n))) ∧ (loopinvariant a a1 (0%Z - 1%Z) (0%Z - 1%Z) n -> is_permutation a1 ∧ (∀(i : Z), 0%Z ≤ i ∧ i < Z.of_nat (length a1) -> nth (Z.to_nat (nth (Z.to_nat i) a1 inhabitant)) a inhabitant = i)))) ∧ (o1 < 0%Z - 1%Z -> is_permutation a ∧ (∀(i : Z), 0%Z ≤ i ∧ i < Z.of_nat (length a) -> nth (Z.to_nat (nth (Z.to_nat i) a inhabitant)) a inhabitant = i)).
Admitted.
