aboutsummaryrefslogtreecommitdiffhomepage
path: root/plugins/setoid_ring/BinList.v
blob: 37eb5899a8c7aa5974a99c02e9353c4821b658b8 (plain)
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
(************************************************************************)
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
(* <O___,, *   INRIA - CNRS - LIX - LRI - PPS - Copyright 1999-2017     *)
(*   \VV/  **************************************************************)
(*    //   *      This file is distributed under the terms of the       *)
(*         *       GNU Lesser General Public License Version 2.1        *)
(************************************************************************)

Require Import BinPos.
Require Export List.
Set Implicit Arguments.
Local Open Scope positive_scope.

Section MakeBinList.
 Variable A : Type.
 Variable default : A.

 Fixpoint jump (p:positive) (l:list A) {struct p} : list A :=
  match p with
  | xH => tl l
  | xO p => jump p (jump p l)
  | xI p  => jump p (jump p (tl l))
  end.

 Fixpoint nth (p:positive) (l:list A) {struct p} : A:=
  match p with
  | xH => hd default l
  | xO p => nth p (jump p l)
  | xI p => nth p (jump p (tl l))
  end.

 Lemma jump_tl : forall j l, tl (jump j l) = jump j (tl l).
 Proof.
  induction j;simpl;intros; now rewrite ?IHj.
 Qed.

 Lemma jump_succ : forall j l,
  jump (Pos.succ j) l = jump 1 (jump j l).
 Proof.
  induction j;simpl;intros.
  - rewrite !IHj; simpl; now rewrite !jump_tl.
  - now rewrite !jump_tl.
  - trivial.
 Qed.

 Lemma jump_add : forall i j l,
  jump (i + j) l = jump i (jump j l).
 Proof.
  induction i using Pos.peano_ind; intros.
  - now rewrite Pos.add_1_l, jump_succ.
  - now rewrite Pos.add_succ_l, !jump_succ, IHi.
 Qed.

 Lemma jump_pred_double : forall i l,
  jump (Pos.pred_double i) (tl l) = jump i (jump i l).
 Proof.
  induction i;intros;simpl.
  - now rewrite !jump_tl.
  - now rewrite IHi, <- 2 jump_tl, IHi.
  - trivial.
 Qed.

 Lemma nth_jump : forall p l, nth p (tl l) = hd default (jump p l).
 Proof.
  induction p;simpl;intros.
  - now rewrite <-jump_tl, IHp.
  - now rewrite <-jump_tl, IHp.
  - trivial.
 Qed.

 Lemma nth_pred_double :
  forall p l, nth (Pos.pred_double p) (tl l) = nth p (jump p l).
 Proof.
  induction p;simpl;intros.
  - now rewrite !jump_tl.
  - now rewrite jump_pred_double, <- !jump_tl, IHp.
  - trivial.
 Qed.

End MakeBinList.