File: NestedInd.v

package info (click to toggle)
coq 8.20.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 44,116 kB
  • sloc: ml: 234,160; sh: 4,301; python: 3,270; ansic: 2,644; makefile: 882; lisp: 172; javascript: 63; xml: 24; sed: 2
file content (188 lines) | stat: -rw-r--r-- 5,933 bytes parent folder | download | duplicates (2)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Require Import Utf8.
Unset Elimination Schemes.

Definition Decision (P : Prop) := {P} + {¬ P}.

Definition RelDecision {A B : Type} (R : A → B → Prop) :=
   ∀ (x : A) (y : B), Decision (R x y) : Type.

Inductive gmap_dep_ne (A : Type) : Type :=
    GNode001 : gmap_dep_ne A → gmap_dep_ne A
  | GNode010 : A → gmap_dep_ne A
  | GNode011 : A → gmap_dep_ne A → gmap_dep_ne A
  | GNode100 : gmap_dep_ne A → gmap_dep_ne A
  | GNode101 : gmap_dep_ne A → gmap_dep_ne A → gmap_dep_ne A
  | GNode110 : gmap_dep_ne A → A → gmap_dep_ne A
  | GNode111 : gmap_dep_ne A → A → gmap_dep_ne A → gmap_dep_ne A.

Variant gmap_dep (A : Type) : Type :=
    GEmpty : gmap_dep A | GNodes : gmap_dep_ne A → gmap_dep A.

Arguments GEmpty {A}.
Arguments GNodes {A}.

Record gmap {K : Type} (EqDecision0 : RelDecision (@eq K))
  (A : Type) : Type := GMap { gmap_car : gmap_dep A }.

Inductive gtest {K : Type} (H : RelDecision (@eq K)) := GTest : gmap H (gtest H) → gtest H.
Arguments GTest {_ _} _.

Definition option_union_with (A : Type) (f : A → A → option A) (mx my : option A) :=
  match mx with
  | Some x => match my with
              | Some y => f x y
              | None => Some x
              end
  | None => match my with
            | Some y => Some y
            | None => None
            end
  end.

Definition gmap_dep_ne_case (A : Type) (B : Type) (t : gmap_dep_ne A)
  (f : gmap_dep A → option A → gmap_dep A → B) :=
  match t with
  | GNode001 _ r => f GEmpty None (GNodes r)
  | GNode010 _ x => f GEmpty (Some (x)) GEmpty
  | GNode011 _ x r => f GEmpty (Some (x)) (GNodes r)
  | GNode100 _ l => f (GNodes l) None GEmpty
  | GNode101 _ l r => f (GNodes l) None (GNodes r)
  | GNode110 _ l x => f (GNodes l) (Some (x)) GEmpty
  | GNode111 _ l x r => f (GNodes l) (Some (x)) (GNodes r)
  end.

Definition gmap_dep_omap_aux {A B : Type} (go : gmap_dep_ne A → gmap_dep B)
  (tm : gmap_dep A) :=
  match tm with
  | GEmpty => GEmpty
  | GNodes t' => go t'
  end.

Definition option_bind {A B : Type} (f : A → option B) (mx : option A) :=
  match mx with
  | Some x => f x
  | None => None
  end.

Definition GNode (A : Type)
  (ml : gmap_dep A)
  (mx : option (A))
  (mr : gmap_dep A) :=
  match ml with
  | GEmpty =>
      match mx with
      | Some x =>
          match mr with
          | GEmpty => GNodes (GNode010 _ x)
          | GNodes r => GNodes (GNode011 _ x r)
          end
      | None =>
          match mr with
          | GEmpty => GEmpty
          | GNodes r => GNodes (GNode001 _ r)
          end
      end
  | GNodes l =>
      match mx with
      | Some (x) =>
          match mr with
          | GEmpty => GNodes (GNode110 _ l x)
          | GNodes r => GNodes (GNode111 _ l x r)
          end
      | None =>
          match mr with
          | GEmpty => GNodes (GNode100 _ l)
          | GNodes r => GNodes (GNode101 _ l r)
          end
      end
  end.

Definition gmap_dep_ne_omap (A B : Type) (f : A → option B) :=
  fix go (t : gmap_dep_ne A) {struct t} : gmap_dep B :=
    gmap_dep_ne_case _ _ t
      (λ (ml : gmap_dep A) (mx : option A) (mr : gmap_dep A),
         GNode _
           (gmap_dep_omap_aux (go) ml)
           (option_bind f mx)
           (gmap_dep_omap_aux (go) mr)).

Definition gmap_merge_aux (A B C : Type) (go : gmap_dep_ne A
                                             → gmap_dep_ne B → gmap_dep C)
  (f : option A → option B → option C) (mt1 : gmap_dep A)
  (mt2 : gmap_dep B) :=
  match mt1 with
  | GEmpty =>
      match mt2 with
      | GEmpty => GEmpty
      | GNodes t2' => gmap_dep_ne_omap _ _ (λ x : B, f None (Some x)) t2'
      end
  | GNodes t1' =>
      match mt2 with
      | GEmpty => gmap_dep_ne_omap _ _ (λ x : A, f (Some x) None) t1'
      | GNodes t2' => go t1' t2'
      end
  end.

Definition diag_None' {A B C : Type} (f : option A → option B → option C)
  (mx : option (A)) (my : option (B)) :=
  match mx with
  | Some (x) =>
      match my with
      | Some (y) => f (Some x) (Some y)
      | None => f (Some x) None
      end
  | None =>
      match my with
      | Some (y) => f None (Some y)
      | None => None
      end
  end.

Definition gmap_dep_ne_merge {A B C : Type} (f : option A → option B → option C) :=
  fix go
   (t1 : gmap_dep_ne A) (t2 : gmap_dep_ne B)
    {struct t1} : gmap_dep C :=
    gmap_dep_ne_case _ _ t1
      (λ (ml1 : gmap_dep A)
         (mx1 : option (A)) (mr1 : gmap_dep A) ,
         gmap_dep_ne_case _ _ t2
           (λ (ml2 : gmap_dep B)
              (mx2 : option (B)) (mr2 : gmap_dep B),
              GNode _
                (gmap_merge_aux _ _ _ go
                   f ml1 ml2) (diag_None' f mx1 mx2)
                (gmap_merge_aux _ _ _ go
                   f mr1 mr2))).

Definition gmap_dep_merge {A B C : Type}
  (f : option A → option B → option C) :=
  gmap_merge_aux _ _ _ (gmap_dep_ne_merge f) f
     : gmap_dep A → gmap_dep B → gmap_dep C.

Definition gmap_merge (K : Type) (H : RelDecision (@eq K))
  (A B C : Type) (f : option A → option B → option C) := (fun '{| gmap_car := mt1 |} '{| gmap_car := mt2 |} =>
  {| gmap_car := gmap_dep_merge f mt1 mt2 |})
     : (gmap H A) -> (gmap H B) -> (gmap H C).

Fixpoint gtest_merge {K : Type} (H : RelDecision (@eq K)) (t1 t2 : gtest H) {struct t1} : gtest H :=
  match t1, t2 with
  | GTest ts1, GTest ts2 =>
     GTest (gmap_merge K H _ _ _ (@option_union_with _ (λ t1 t2, Some (gtest_merge H t1 t2))) ts1 ts2)
  end.

(* An example from metacoq (simplified) *)

Notation "x .π2" := (projT2 x) (at level 0).

Parameter term : Type.

Inductive All (P :  term -> Type) : Type :=
  | All_cons : {x:term & P x} -> All P -> All P.

Inductive inferring  (Σ : unit) : term -> Type :=
| infer x : All (inferring Σ) -> inferring Σ x.

Fixpoint inferring_size  {Σ t} (d : inferring Σ t) {struct d} : nat :=
  match d with
  | infer _ _ (All_cons _ p _) => inferring_size p.π2
  end.