| 1 |
Unset Elimination Schemes. |
| 2 |
|
| 3 |
Definition Decision (P : Prop) := {P} + {~ P}. |
| 4 |
|
| 5 |
Definition RelDecision {A B : Type} (R : A -> B -> Prop) := |
| 6 |
forall (x : A) (y : B), Decision (R x y) : Type. |
| 7 |
|
| 8 |
Inductive gmap_dep_ne (A : Type) : Type := |
| 9 |
GNode001 : gmap_dep_ne A -> gmap_dep_ne A |
| 10 |
| GNode010 : A -> gmap_dep_ne A |
| 11 |
| GNode011 : A -> gmap_dep_ne A -> gmap_dep_ne A |
| 12 |
| GNode100 : gmap_dep_ne A -> gmap_dep_ne A |
| 13 |
| GNode101 : gmap_dep_ne A -> gmap_dep_ne A -> gmap_dep_ne A |
| 14 |
| GNode110 : gmap_dep_ne A -> A -> gmap_dep_ne A |
| 15 |
| GNode111 : gmap_dep_ne A -> A -> gmap_dep_ne A -> gmap_dep_ne A. |
| 16 |
|
| 17 |
Variant gmap_dep (A : Type) : Type := |
| 18 |
GEmpty : gmap_dep A | GNodes : gmap_dep_ne A -> gmap_dep A. |
| 19 |
|
| 20 |
Arguments GEmpty {A}. |
| 21 |
Arguments GNodes {A}. |
| 22 |
|
| 23 |
Record gmap {K : Type} (EqDecision0 : RelDecision (@eq K)) |
| 24 |
(A : Type) : Type := GMap { gmap_car : gmap_dep A }. |
| 25 |
|
| 26 |
Inductive gtest {K : Type} (H : RelDecision (@eq K)) := GTest : gmap H (gtest H) -> gtest H. |
| 27 |
Arguments GTest {_ _} _. |
| 28 |
|
| 29 |
Definition option_union_with (A : Type) (f : A -> A -> option A) (mx my : option A) := |
| 30 |
match mx with |
| 31 |
| Some x => match my with |
| 32 |
| Some y => f x y |
| 33 |
| None => Some x |
| 34 |
end |
| 35 |
| None => match my with |
| 36 |
| Some y => Some y |
| 37 |
| None => None |
| 38 |
end |
| 39 |
end. |
| 40 |
|
| 41 |
Definition gmap_dep_ne_case (A : Type) (B : Type) (t : gmap_dep_ne A) |
| 42 |
(f : gmap_dep A -> option A -> gmap_dep A -> B) := |
| 43 |
match t with |
| 44 |
| GNode001 _ r => f GEmpty None (GNodes r) |
| 45 |
| GNode010 _ x => f GEmpty (Some (x)) GEmpty |
| 46 |
| GNode011 _ x r => f GEmpty (Some (x)) (GNodes r) |
| 47 |
| GNode100 _ l => f (GNodes l) None GEmpty |
| 48 |
| GNode101 _ l r => f (GNodes l) None (GNodes r) |
| 49 |
| GNode110 _ l x => f (GNodes l) (Some (x)) GEmpty |
| 50 |
| GNode111 _ l x r => f (GNodes l) (Some (x)) (GNodes r) |
| 51 |
end. |
| 52 |
|
| 53 |
Definition gmap_dep_omap_aux {A B : Type} (go : gmap_dep_ne A -> gmap_dep B) |
| 54 |
(tm : gmap_dep A) := |
| 55 |
match tm with |
| 56 |
| GEmpty => GEmpty |
| 57 |
| GNodes t' => go t' |
| 58 |
end. |
| 59 |
|
| 60 |
Definition option_bind {A B : Type} (f : A -> option B) (mx : option A) := |
| 61 |
match mx with |
| 62 |
| Some x => f x |
| 63 |
| None => None |
| 64 |
end. |
| 65 |
|
| 66 |
Definition GNode (A : Type) |
| 67 |
(ml : gmap_dep A) |
| 68 |
(mx : option (A)) |
| 69 |
(mr : gmap_dep A) := |
| 70 |
match ml with |
| 71 |
| GEmpty => |
| 72 |
match mx with |
| 73 |
| Some x => |
| 74 |
match mr with |
| 75 |
| GEmpty => GNodes (GNode010 _ x) |
| 76 |
| GNodes r => GNodes (GNode011 _ x r) |
| 77 |
end |
| 78 |
| None => |
| 79 |
match mr with |
| 80 |
| GEmpty => GEmpty |
| 81 |
| GNodes r => GNodes (GNode001 _ r) |
| 82 |
end |
| 83 |
end |
| 84 |
| GNodes l => |
| 85 |
match mx with |
| 86 |
| Some (x) => |
| 87 |
match mr with |
| 88 |
| GEmpty => GNodes (GNode110 _ l x) |
| 89 |
| GNodes r => GNodes (GNode111 _ l x r) |
| 90 |
end |
| 91 |
| None => |
| 92 |
match mr with |
| 93 |
| GEmpty => GNodes (GNode100 _ l) |
| 94 |
| GNodes r => GNodes (GNode101 _ l r) |
| 95 |
end |
| 96 |
end |
| 97 |
end. |
| 98 |
|
| 99 |
Definition gmap_dep_ne_omap (A B : Type) (f : A -> option B) := |
| 100 |
fix go (t : gmap_dep_ne A) {struct t} : gmap_dep B := |
| 101 |
gmap_dep_ne_case _ _ t |
| 102 |
(fun (ml : gmap_dep A) (mx : option A) (mr : gmap_dep A) => |
| 103 |
GNode _ |
| 104 |
(gmap_dep_omap_aux (go) ml) |
| 105 |
(option_bind f mx) |
| 106 |
(gmap_dep_omap_aux (go) mr)). |
| 107 |
|
| 108 |
Definition gmap_merge_aux (A B C : Type) (go : gmap_dep_ne A |
| 109 |
-> gmap_dep_ne B -> gmap_dep C) |
| 110 |
(f : option A -> option B -> option C) (mt1 : gmap_dep A) |
| 111 |
(mt2 : gmap_dep B) := |
| 112 |
match mt1 with |
| 113 |
| GEmpty => |
| 114 |
match mt2 with |
| 115 |
| GEmpty => GEmpty |
| 116 |
| GNodes t2' => gmap_dep_ne_omap _ _ (fun x : B => f None (Some x)) t2' |
| 117 |
end |
| 118 |
| GNodes t1' => |
| 119 |
match mt2 with |
| 120 |
| GEmpty => gmap_dep_ne_omap _ _ (fun x : A => f (Some x) None) t1' |
| 121 |
| GNodes t2' => go t1' t2' |
| 122 |
end |
| 123 |
end. |
| 124 |
|
| 125 |
Definition diag_None' {A B C : Type} (f : option A -> option B -> option C) |
| 126 |
(mx : option (A)) (my : option (B)) := |
| 127 |
match mx with |
| 128 |
| Some (x) => |
| 129 |
match my with |
| 130 |
| Some (y) => f (Some x) (Some y) |
| 131 |
| None => f (Some x) None |
| 132 |
end |
| 133 |
| None => |
| 134 |
match my with |
| 135 |
| Some (y) => f None (Some y) |
| 136 |
| None => None |
| 137 |
end |
| 138 |
end. |
| 139 |
|
| 140 |
Definition gmap_dep_ne_merge {A B C : Type} (f : option A -> option B -> option C) := |
| 141 |
fix go |
| 142 |
(t1 : gmap_dep_ne A) (t2 : gmap_dep_ne B) |
| 143 |
{struct t1} : gmap_dep C := |
| 144 |
gmap_dep_ne_case _ _ t1 |
| 145 |
(fun (ml1 : gmap_dep A) |
| 146 |
(mx1 : option (A)) (mr1 : gmap_dep A) => |
| 147 |
gmap_dep_ne_case _ _ t2 |
| 148 |
(fun (ml2 : gmap_dep B) |
| 149 |
(mx2 : option (B)) (mr2 : gmap_dep B) => |
| 150 |
GNode _ |
| 151 |
(gmap_merge_aux _ _ _ go |
| 152 |
f ml1 ml2) (diag_None' f mx1 mx2) |
| 153 |
(gmap_merge_aux _ _ _ go |
| 154 |
f mr1 mr2))). |
| 155 |
|
| 156 |
Definition gmap_dep_merge {A B C : Type} |
| 157 |
(f : option A -> option B -> option C) := |
| 158 |
gmap_merge_aux _ _ _ (gmap_dep_ne_merge f) f |
| 159 |
: gmap_dep A -> gmap_dep B -> gmap_dep C. |
| 160 |
|
| 161 |
Definition gmap_merge (K : Type) (H : RelDecision (@eq K)) |
| 162 |
(A B C : Type) (f : option A -> option B -> option C) := (fun '{| gmap_car := mt1 |} '{| gmap_car := mt2 |} => |
| 163 |
{| gmap_car := gmap_dep_merge f mt1 mt2 |}) |
| 164 |
: (gmap H A) -> (gmap H B) -> (gmap H C). |
| 165 |
|
| 166 |
Fixpoint gtest_merge {K : Type} (H : RelDecision (@eq K)) (t1 t2 : gtest H) {struct t1} : gtest H := |
| 167 |
match t1, t2 with |
| 168 |
| GTest ts1, GTest ts2 => |
| 169 |
GTest (gmap_merge K H _ _ _ (@option_union_with _ (fun t1 t2 => Some (gtest_merge H t1 t2))) ts1 ts2) |
| 170 |
end. |
| 171 |
|
| 172 |
(* An example from metacoq (simplified) *) |
| 173 |
|
| 174 |
Notation "x .π2" := (projT2 x) (at level 0). |
| 175 |
|
| 176 |
Parameter term : Type. |
| 177 |
|
| 178 |
Inductive All (P : term -> Type) : Type := |
| 179 |
| All_cons : {x:term & P x} -> All P -> All P. |
| 180 |
|
| 181 |
Inductive inferring (Σ : unit) : term -> Type := |
| 182 |
| infer x : All (inferring Σ) -> inferring Σ x. |
| 183 |
|
| 184 |
Fixpoint inferring_size {Σ t} (d : inferring Σ t) {struct d} : nat := |
| 185 |
match d with |
| 186 |
| infer _ _ (All_cons _ p _) => inferring_size p.π2 |
| 187 |
end. |