1+ Require Import ExtLib.Data.Map.FMapPositive.
2+
3+ Fixpoint pmap_lookup' (ts : pmap Type ) (p : positive) : option Type :=
4+ match p with
5+ | xH => pmap_here ts
6+ | xI p => pmap_lookup' (pmap_right ts) p
7+ | xO p => pmap_lookup' (pmap_left ts) p
8+ end .
9+
10+ Record OneOf (ts : pmap Type ) : Type :=
11+ { index : positive
12+ ; value : match pmap_lookup' ts index with
13+ | None => Empty_set
14+ | Some T => T
15+ end
16+ }.
17+
18+ Definition Into {ts} {T : Type } (v : T) (n : positive) (pf : Some T = pmap_lookup' ts n) : OneOf ts :=
19+ {| index := n
20+ ; value := match pf in _ = z return match z with
21+ | None => Empty_set
22+ | Some T => T
23+ end
24+ with
25+ | eq_refl => v
26+ end
27+ |}.
28+
29+ Fixpoint asNth' {ts : pmap Type } (p p' : positive)
30+ : match pmap_lookup' ts p' with
31+ | None => Empty_set
32+ | Some T => T
33+ end -> option (match pmap_lookup' ts p with
34+ | None => Empty_set
35+ | Some T => T
36+ end ) :=
37+ match p as p , p' as p'
38+ return match pmap_lookup' ts p' with
39+ | None => Empty_set
40+ | Some T => T
41+ end -> option (match pmap_lookup' ts p with
42+ | None => Empty_set
43+ | Some T => T
44+ end )
45+ with
46+ | xH , xH => Some
47+ | xI p , xI p' => asNth' p p'
48+ | xO p , xO p' => asNth' p p'
49+ | _ , _ => fun _ => None
50+ end .
51+
52+ Definition asNth {ts : pmap Type } (p : positive) (oe : OneOf ts)
53+ : option (match pmap_lookup' ts p with
54+ | None => Empty_set
55+ | Some T => T
56+ end ) :=
57+ @asNth' ts p oe.(index ts) oe.(value ts).
58+
59+ Theorem asNth_eq
60+ : forall ts p oe v,
61+ @asNth ts p oe = Some v ->
62+ oe = {| index := p ; value := v |}.
63+ Proof .
64+ unfold asNth.
65+ destruct oe; simpl.
66+ revert value0. revert index0. revert ts.
67+ induction p; destruct index0; simpl; intros;
68+ solve [ congruence | eapply IHp in H; inversion H; clear H IHp; subst; auto ].
69+ Qed .
70+
71+ Section elim.
72+ Context {T : Type}.
73+ Variable F : T -> Type.
74+
75+ Fixpoint pmap_elim (R : Type ) (ts : pmap T) : Type :=
76+ match ts with
77+ | Empty => R
78+ | Branch None l r => pmap_elim (pmap_elim R r) l
79+ | Branch (Some x) l r => F x -> pmap_elim (pmap_elim R r) l
80+ end .
81+ End elim.
82+
83+ (*
84+ Fixpoint matchOn' T {ts : pmap Type} (p : positive) {struct ts}
85+ : match pmap_lookup p ts with
86+ | None => Empty_set
87+ | Some T => T
88+ end -> pmap_elim (fun x => x -> T) T ts.
89+ refine
90+ match ts as ts
91+ , p as p
92+ return match pmap_lookup p ts with
93+ | None => Empty_set
94+ | Some T => T
95+ end -> pmap_elim (fun x => x -> T) T ts with
96+ | Empty , xH => fun x : Empty_set => match x with end
97+ | Empty , xO _ => fun x : Empty_set => match x with end
98+ | Empty , xI _ => fun x : Empty_set => match x with end
99+ | Branch None l r , xH => _
100+ | Branch (Some T) l r , xH => _
101+ | Branch b l r , xO p => _
102+ | Branch b l r , xI p => _
103+ end; simpl.
104+ Focus 3.
105+ *)
0 commit comments