File: bug_18680.v

package info (click to toggle)
rocq-stdlib 9.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 11,828 kB
  • sloc: python: 2,928; sh: 444; makefile: 319; javascript: 24; ml: 2
file content (63 lines) | stat: -rw-r--r-- 1,520 bytes parent folder | download
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
Set Universe Polymorphism.

From Stdlib Require Import PeanoNat.
From Stdlib Require Import JMeq.

Inductive narray {X : Type} : nat -> Type :=
    | Elt : X -> narray 0
    | Cons {c : nat} (x : X) (n : narray c) : narray (c + 1).

Lemma eqb_eq : forall x, Nat.eqb x x = true.
Proof.
  intros.
  induction x.
  - reflexivity.
  - simpl. apply IHx.
Qed.

Lemma sub_still_le : forall x y,
  Nat.eqb x y = false -> x <= y -> x + 1 <= y.
Proof.
  intros x y NatEq xLey.
  induction xLey.
  - rewrite eqb_eq in NatEq. discriminate NatEq.
  - replace (x + 1) with (S x).
    * apply le_n_S. apply xLey.
    * rewrite Nat.add_comm. simpl. reflexivity.
Qed.

(* Returns the nth element of the array; this version works. *)
Program Fixpoint get
   {X : Type} {sz : nat}
   (get_n : nat) (cpt : nat) (l : narray sz) : X :=
    match l with
    | Elt x => x
    | Cons x tl =>
      match Nat.eqb get_n cpt with
      | true => x
      | false =>
        get get_n (cpt + 1) tl
    end
  end.

(* Same than get, but carries additional proofs. *)
Succeed Program Fixpoint get'
   {X : Type} {sz : nat}
   (get_n : nat)
   (Inv : get_n < sz)
   (cpt : nat)
   (P : cpt <= get_n)
   (l : narray sz) : X :=
    match l with
    | Elt x => x
    | Cons x tl =>
      match Nat.eqb get_n cpt with
      | true => x
      | false =>
        let CondFalse: Nat.eqb get_n cpt = false := _ in
        let P' : cpt + 1 <= get_n :=
          sub_still_le cpt get_n CondFalse P
        in
        get' get_n Inv (cpt + 1) P' tl
    end
  end.