better names for the auxiliary CaseBranch records
[coq-hetmet.git] / src / HaskStrongTypes.v
1 (*********************************************************************************************************************************)
2 (* HaskStrongTypes: representation of types and coercions for HaskStrong                                                         *)
3 (*********************************************************************************************************************************)
4
5 Generalizable All Variables.
6 Require Import Preamble.
7 Require Import General.
8 Require Import Coq.Strings.String.
9 Require Import Coq.Lists.List.
10 Require Import HaskGeneral.
11 Require Import HaskLiterals.
12
13 (* types prefixed with "Raw" are NOT binder-polymorphic; they have had their PHOAS parameter instantiated already *)
14 Section Raw.
15
16   (* TV is the PHOAS type which stands for type variables of System FC *)
17   Context {TV:Type}.
18
19   (* Figure 7: ρ, σ, τ, ν *)
20   Inductive RawHaskType  : Type :=
21   | TVar           : TV                                                     -> RawHaskType   (* a        *)
22   | TCon           : ∀n, TyCon n                                            -> RawHaskType   (* T        *)
23   | TCoerc         : Kind                                                   -> RawHaskType   (* (+>)     *)
24   | TApp           : RawHaskType                  ->     RawHaskType        -> RawHaskType   (* φ φ      *)
25   | TFCApp         : ∀n, TyFunConst n             -> vec RawHaskType n      -> RawHaskType   (* S_n      *)
26   | TAll           : Kind  -> (TV -> RawHaskType)                           -> RawHaskType   (* ∀a:κ.φ   *)
27   | TCode          : RawHaskType -> RawHaskType                             -> RawHaskType   (* from λ^α *).
28
29   (* the "kind" of a coercion is a pair of types *)
30   Inductive RawCoercionKind : Type := mkRawCoercionKind : RawHaskType -> RawHaskType -> RawCoercionKind.
31
32   Section CV.
33
34     (* the PHOAS type which stands for coercion variables of System FC *)
35     Context {CV:Type}.
36
37     (* Figure 7: γ, δ *)
38     Inductive RawHaskCoer : Prop :=
39     | CoVar          : CV                                           -> RawHaskCoer (* g      *)
40     | CoType         : RawHaskType                                  -> RawHaskCoer (* τ      *)
41     | CoApp          : RawHaskCoer -> RawHaskCoer                   -> RawHaskCoer (* γ γ    *)
42     | CoAppT         : RawHaskCoer -> RawHaskType                   -> RawHaskCoer (* γ@v    *)
43     | CoCFApp        : ∀n, CoFunConst n -> vec RawHaskCoer n        -> RawHaskCoer (* C   γⁿ *)
44     | CoTFApp        : ∀n, TyFunConst n -> vec RawHaskCoer n        -> RawHaskCoer (* S_n γⁿ *)
45     | CoAll          : Kind  -> (TV -> RawHaskCoer)                 -> RawHaskCoer (* ∀a:κ.γ *)
46     | CoSym          : RawHaskCoer                                  -> RawHaskCoer (* sym    *)
47     | CoComp         : RawHaskCoer -> RawHaskCoer                   -> RawHaskCoer (* ◯      *)
48     | CoLeft         : RawHaskCoer                                  -> RawHaskCoer (* left   *)
49     | CoRight        : RawHaskCoer                                  -> RawHaskCoer (* right  *).
50
51   End CV.
52 End Raw.
53
54 Implicit Arguments TCon   [ [TV] [n]].
55 Implicit Arguments TFCApp [ [TV] [n]].
56 Implicit Arguments RawHaskType  [ ].
57 Implicit Arguments RawHaskCoer  [ ].
58 Implicit Arguments RawCoercionKind [ ].
59
60 Notation "t1 ---> t2"        := (fun TV env => (@TApp _ (@TApp _ (@TCon TV _ ArrowTyCon) (t1 TV env)) (t2 TV env))).
61 Notation "φ₁ ∼∼ φ₂ : κ ⇒ φ₃" :=
62    (fun TV env => @TApp _ (@TApp _ (@TCon _ _ ArrowTyCon) (@TApp _ (@TApp _ (@TCoerc _ κ) (φ₁ TV env)) (φ₂ TV env))) (φ₃ TV env)).
63
64
65
66 (* Kind and Coercion Environments *)
67 (*
68  *  In System FC, the environment consists of three components, each of
69  *  whose well-formedness depends on all of those prior to it:
70  *
71  *    1. (TypeEnv)        The list of free type     variables and their kinds
72  *    2. (CoercionEnv)    The list of free coercion variables and the pair of types between which it witnesses coercibility
73  *    3. (Tree ??CoreVar) The list of free value    variables and the type of each one
74  *)
75
76 Definition TypeEnv                                                         := list Kind.
77 Definition InstantiatedTypeEnv     (TV:Type)   (Γ:TypeEnv)                 := vec TV (length Γ).
78 Definition HaskCoercionKind                    (Γ:TypeEnv)                 := ∀ TV, InstantiatedTypeEnv TV Γ -> @RawCoercionKind TV.
79 Definition CoercionEnv                         (Γ:TypeEnv)                 := list (HaskCoercionKind Γ).
80 Definition InstantiatedCoercionEnv (TV CV:Type)(Γ:TypeEnv)(Δ:CoercionEnv Γ):= vec CV (length Δ).
81
82
83 (* A (HaskXX Γ) is an XX which is valid in environments of shape Γ; they are always PHOAS-uninstantiated *)
84 Definition HaskTyVar (Γ:TypeEnv) :=  forall TV    (env:@InstantiatedTypeEnv TV Γ), TV.
85 Definition HaskCoVar Γ Δ         :=  forall TV CV (env:@InstantiatedTypeEnv TV Γ)(cenv:@InstantiatedCoercionEnv TV CV Γ Δ), CV.
86 Definition HaskLevel (Γ:TypeEnv) :=  list (HaskTyVar Γ).
87 Definition HaskType         (Γ:TypeEnv)                  := ∀ TV, @InstantiatedTypeEnv TV    Γ -> RawHaskType           TV.
88 Definition HaskCoercion Γ Δ := ∀ TV CV,
89   @InstantiatedTypeEnv TV Γ -> @InstantiatedCoercionEnv TV CV Γ Δ -> RawHaskCoer TV CV.
90 Inductive  LeveledHaskType (Γ:TypeEnv) := mkLeveledHaskType : HaskType Γ -> HaskLevel Γ -> LeveledHaskType Γ.
91 Definition FreshHaskTyVar {Γ}(κ:Kind)  : HaskTyVar (κ::Γ) := fun TV env => vec_head env.
92 Definition HaskTAll {Γ}(κ:Kind)(σ:forall TV (env:@InstantiatedTypeEnv TV Γ), TV -> RawHaskType TV) : HaskType Γ
93   := fun TV env => TAll κ (σ TV env).
94 Definition HaskTApp {Γ}(σ:forall TV (env:@InstantiatedTypeEnv TV Γ), TV -> RawHaskType TV)(cv:HaskTyVar Γ) : HaskType Γ
95   := fun TV env => σ TV env (cv TV env).
96 Definition HaskBrak {Γ}(v:HaskTyVar Γ)(t:HaskType Γ) : HaskType Γ := fun TV env => @TCode TV (TVar (v TV env)) (t TV env).
97 Definition HaskTCon {Γ}{n}(tc:TyCon n) : HaskType Γ := fun TV ite => TCon tc.
98 Definition HaskAppT {Γ}(t1 t2:HaskType Γ) : HaskType Γ := fun TV ite => TApp (t1 TV ite) (t2 TV ite).
99 Definition mkHaskCoercionKind {Γ}(t1 t2:HaskType Γ) : HaskCoercionKind Γ :=
100  fun TV ite => mkRawCoercionKind (t1 TV ite) (t2 TV ite).
101
102
103 (* PHOAS substitution on types *)
104 Definition substT {Γ}(exp:forall TV (env:@InstantiatedTypeEnv TV Γ), TV -> RawHaskType TV)(v:@HaskType Γ) : @HaskType Γ
105   := fun TV env =>
106     (fix flattenT (exp: RawHaskType (RawHaskType TV)) : RawHaskType TV :=
107      match exp with
108     | TVar       x        => x
109     | TAll       k y      => TAll      k (fun v => flattenT (y (TVar v)))
110     | TApp       x y      => TApp      (flattenT x) (flattenT y)
111     | TCon   n   tc       => TCon      tc
112     | TCoerc     k        => TCoerc    k
113     | TCode      v e      => TCode     (flattenT v) (flattenT e)
114     | TFCApp     n tfc lt => TFCApp    tfc
115       ((fix flatten_vec n (expv:vec (RawHaskType (RawHaskType TV)) n) : vec (RawHaskType TV) n :=
116         match expv in vec _ N return vec (RawHaskType TV) N with
117           | vec_nil => vec_nil
118           | x:::y   => (flattenT x):::(flatten_vec _ y)
119         end) n lt)
120     end)
121     (exp (RawHaskType TV) (vec_map (fun tv => TVar tv) env) (v TV env)).
122 Notation "t @@  l" := (@mkLeveledHaskType _ t l) (at level 20).
123 Notation "t @@@ l" := (mapOptionTree (fun t' => t' @@ l) t) (at level 20).
124 Notation "'<[' a '|-' t ']>'" := (@HaskBrak _ a t).
125
126
127
128 (* like a GHC DataCon, but using PHOAS representation for types and coercions *)
129 Record StrongAltConInfo {n}{tc:TyCon n} :=
130 { saci_numExTyVars :  nat
131 ; saci_numCoerVars :  nat
132 ; saci_numExprVars :  nat
133 ; saci_akinds      :  vec Kind n
134 ; saci_ekinds      :  vec Kind saci_numExTyVars
135 ; saci_kinds       := app (vec2list saci_akinds) (vec2list saci_ekinds)
136 ; saci_coercions   :  vec (HaskType saci_kinds * HaskType saci_kinds) saci_numCoerVars
137 ; saci_types       :  vec (HaskType saci_kinds) saci_numExprVars
138 }.
139 Implicit Arguments StrongAltConInfo [[n]].
140
141 Definition caseType {Γ:TypeEnv}{n}(tc:TyCon n)(atypes:vec (HaskType Γ) n) :=
142   fold_left HaskAppT (vec2list atypes) (HaskTCon(Γ:=Γ) tc).
143
144
145
146
147
148
149
150
151
152
153
154 (* FIXME: it's a mess below this point *)
155
156
157 Definition unAddKindFromInstantiatedTypeEnv {Γ:TypeEnv}{κ:Kind}{TV:Type}(ite:InstantiatedTypeEnv TV (κ::Γ)) := vec_tail ite.
158 Definition addKindToCoercionEnv (Γ:TypeEnv)(Δ:CoercionEnv Γ)(κ:Kind) : CoercionEnv (κ::Γ) :=
159   map (fun f => (fun TV ite => f TV (unAddKindFromInstantiatedTypeEnv ite))) Δ.
160 Definition addKindToInstantiatedTypeEnv {Γ:TypeEnv}{TV:Type}(env:InstantiatedTypeEnv TV Γ)(tv:TV)(κ:Kind)
161   : InstantiatedTypeEnv TV (κ::Γ) := tv:::env.
162 Definition addKindToInstantiatedCoercionEnv {Γ:TypeEnv}{Δ}{TV CV:Type}(env:InstantiatedCoercionEnv TV CV Γ Δ)(tv:TV)(κ:Kind)
163   : InstantiatedCoercionEnv TV CV (κ::Γ) (addKindToCoercionEnv Γ Δ κ).
164     simpl.
165     unfold InstantiatedCoercionEnv.
166     unfold addKindToCoercionEnv.
167     simpl.
168     rewrite <- map_preserves_length.
169     apply env.
170     Defined.
171 Definition typeEnvContainsType {Γ}{TV:Type}(env:InstantiatedTypeEnv TV Γ)(tv:TV)(κ:Kind) : Prop
172   := @vec_In _ _ (tv,κ) (vec_zip env (list2vec Γ)).
173 Definition coercionEnvContainsCoercion {Γ}{Δ}{TV CV:Type}(ite:InstantiatedTypeEnv TV Γ)
174   (ice:InstantiatedCoercionEnv TV CV Γ Δ)(cv:CV)(ck:RawCoercionKind TV)
175   := @vec_In _ _ (cv,ck) (vec_zip ice (vec_map (fun f => f TV ite) (list2vec Δ))).
176 Definition addCoercionToCoercionEnv {Γ}(Δ:CoercionEnv Γ)(κ:HaskCoercionKind Γ) : CoercionEnv Γ :=
177   κ::Δ.
178 Definition addCoercionToInstantiatedCoercionEnv {Γ}{Δ}{κ}{TV CV}(ice:InstantiatedCoercionEnv TV CV Γ Δ)(cv:CV)
179   : InstantiatedCoercionEnv TV CV Γ (addCoercionToCoercionEnv Δ κ).
180   simpl.
181   unfold addCoercionToCoercionEnv; simpl.
182   unfold InstantiatedCoercionEnv; simpl. 
183   apply vec_cons; auto.
184   Defined.
185 Notation "env  ∋  tv : κ"        := (@typeEnvContainsType  _ _ env tv κ).
186 Notation "env '+' tv : κ"        := (@addKindToInstantiatedTypeEnv  _ _ env tv κ).
187
188 (* the various "weak" functions turn a HaskXX-in-Γ into a HaskXX-in-(κ::Γ) *)
189 Definition weakITE  {Γ:TypeEnv}{κ}{TV}(ite:InstantiatedTypeEnv TV (κ::Γ)) : InstantiatedTypeEnv TV Γ := vec_tail ite.
190 Definition weakITE' {Γ:TypeEnv}{κ}{TV}(ite:InstantiatedTypeEnv TV (app κ Γ)) : InstantiatedTypeEnv TV Γ.
191   induction κ; auto. apply IHκ. inversion ite; subst. apply X0. Defined.
192 Definition weakCE   {Γ:TypeEnv}{κ}(Δ:CoercionEnv Γ) : CoercionEnv (κ::Γ) := map (fun x => (fun tv ite => x tv (weakITE ite))) Δ.
193 Definition weakV  {Γ:TypeEnv}{κ}(cv':HaskTyVar Γ) : HaskTyVar (κ::Γ) := fun TV ite => (cv' TV (weakITE ite)).
194 Definition weakV' {Γ:TypeEnv}{κ}(cv':HaskTyVar Γ) : HaskTyVar (app κ Γ).
195   induction κ; auto. apply weakV; auto. Defined.
196 Definition weakT {Γ:TypeEnv}{κ:Kind}(lt:HaskType Γ) : HaskType (κ::Γ) := fun TV ite => lt TV (weakITE ite).
197 Definition weakL  {Γ}{κ}(lt:HaskLevel Γ) : HaskLevel (κ::Γ) := map weakV lt.
198 Definition weakT' {Γ}{κ}(lt:HaskType Γ) : HaskType (app κ Γ).
199   induction κ; auto. apply weakT; auto. Defined.
200 Definition weakT'' {Γ}{κ}(lt:HaskType Γ) : HaskType (app Γ κ).
201 unfold HaskType in *.
202 unfold InstantiatedTypeEnv in *.
203 intros.
204 apply vec_chop in X.
205 apply lt.
206 apply X.
207 Defined.
208 Definition lamer {a}{b}{c}(lt:HaskType (app (app a  b) c)) : HaskType  (app a (app b c)).
209 rewrite <- ass_app in lt.
210 exact lt.
211 Defined.
212 Definition weakL' {Γ}{κ}(lev:HaskLevel Γ) : HaskLevel (app κ Γ).
213   induction κ; auto. apply weakL; auto. Defined.
214 Definition weakLT {Γ}{κ}(lt:LeveledHaskType Γ) : LeveledHaskType (κ::Γ) := match lt with t @@ l => weakT t @@ weakL l end.
215 Definition weakLT' {Γ}{κ}(lt:LeveledHaskType Γ) : LeveledHaskType (app κ Γ)
216   := match lt with t @@ l => weakT' t @@ weakL' l end.
217 Definition weakCE' {Γ:TypeEnv}{κ}(Δ:CoercionEnv Γ) : CoercionEnv (app κ Γ).
218   induction κ; auto. apply weakCE; auto. Defined.
219 Definition weakICE  {Γ:TypeEnv}{κ}{Δ:CoercionEnv Γ}{TV}{CV}(ice:InstantiatedCoercionEnv TV CV (κ::Γ) (weakCE Δ))
220   : InstantiatedCoercionEnv TV CV Γ Δ.
221   intros.
222   unfold InstantiatedCoercionEnv; intros.
223   unfold InstantiatedCoercionEnv in ice.
224   unfold weakCE in ice.
225   simpl in ice.
226   rewrite <- map_preserves_length in ice.
227   apply ice.
228   Defined.
229 Definition weakICE' {Γ:TypeEnv}{κ}{Δ:CoercionEnv Γ}{TV}{CV}(ice:InstantiatedCoercionEnv TV CV (app κ Γ) (weakCE' Δ))
230   : InstantiatedCoercionEnv TV CV Γ Δ.
231   induction κ; auto. apply IHκ. eapply weakICE. apply ice. Defined.
232 Definition weakCV {Γ}{Δ}{κ}(cv':HaskCoVar Γ Δ) : HaskCoVar (κ::Γ) (weakCE Δ) :=
233   fun TV CV ite ice => (cv' TV CV (weakITE ite) (weakICE ice)).
234 Definition weakC  {Γ}{κ}{Δ}(c:HaskCoercion Γ Δ) : HaskCoercion (κ::Γ) (weakCE Δ) :=
235   fun TV CV ite ice => c TV CV (weakITE ite) (weakICE ice).
236 Definition weakF {Γ:TypeEnv}{κ:Kind}(f:forall TV (env:@InstantiatedTypeEnv TV Γ), TV -> RawHaskType TV) : 
237   forall TV (env:@InstantiatedTypeEnv TV (κ::Γ)), TV -> RawHaskType TV
238   := fun TV ite tv => (f TV (weakITE ite) tv).
239
240 (***************************************************************************************************)
241 (* Well-Formedness of Types and Coercions                                                          *)
242 (* also represents production "S_n:κ" of Γ because these can only wind up in Γ via rule (Type) *)
243 Inductive TypeFunctionDecl (n:nat)(tfc:TyFunConst n)(vk:vec Kind n) : Type :=
244   mkTFD : Kind -> TypeFunctionDecl n tfc vk.
245
246
247 (* Figure 8, upper half *)
248 Inductive WellKinded_RawHaskType (TV:Type)
249   : forall Γ:TypeEnv, InstantiatedTypeEnv TV Γ -> RawHaskType TV -> Kind -> Prop :=
250   | WFTyVar  : forall Γ env κ d,
251                 env∋d:κ ->
252                 WellKinded_RawHaskType TV Γ env (TVar d) κ
253   | WFTyApp  : forall Γ env κ₁ κ₂ σ₁ σ₂,
254                 WellKinded_RawHaskType TV Γ env σ₁  (κ₁ ⇛ κ₂)  ->
255                 WellKinded_RawHaskType TV Γ env σ₂        κ₂   ->
256                 WellKinded_RawHaskType TV Γ env (TApp σ₁ σ₂) κ₂
257   | WFTyAll  : forall Γ (env:InstantiatedTypeEnv TV Γ) κ     σ    ,
258                 (∀ a, WellKinded_RawHaskType TV _ (@addKindToInstantiatedTypeEnv _ TV env a κ) (σ a) ★ )        -> 
259                 WellKinded_RawHaskType TV _  env      (TAll κ σ) ★ 
260   | TySCon   : forall Γ env n tfc lt vk ι ,
261                 @TypeFunctionDecl n tfc vk  ->
262                 ListWellKinded_RawHaskType TV Γ _ env lt vk ->
263                 WellKinded_RawHaskType TV Γ env (TFCApp tfc lt) ι
264 with ListWellKinded_RawHaskType (TV:Type)
265   : forall (Γ:TypeEnv) n, InstantiatedTypeEnv TV Γ -> vec (RawHaskType TV) n -> vec Kind n -> Prop :=
266   | ListWFRawHaskTypenil  : forall Γ env,
267                 ListWellKinded_RawHaskType TV Γ 0 env  vec_nil vec_nil
268   | ListWFRawHaskTypecons : forall Γ env n t k lt lk,
269                 WellKinded_RawHaskType     TV Γ env      t       k  ->
270                 ListWellKinded_RawHaskType TV Γ n env     lt      lk  ->
271                 ListWellKinded_RawHaskType TV Γ (S n) env  (t:::lt) (k:::lk).
272
273 (*
274 Fixpoint kindOfRawHaskType {TV}(rht:RawHaskType Kind) : Kind :=
275 match rht with
276   | TVar   k    => k
277   | TCon   n tc => ̱★ (* FIXME: use the StrongAltConInfo *)
278   | TCoerc k    => k ⇛ (k ⇛ (★ ⇛ ★ ))
279   | TApp   ht1 ht2        : RawHaskType                  ->     RawHaskType        -> RawHaskType   (* φ φ      *)
280   | TFCApp         : ∀n, TyFunConst n             -> vec RawHaskType n      -> RawHaskType   (* S_n      *)
281   | TAll           : Kind  -> (TV -> RawHaskType)                           -> RawHaskType   (* ∀a:κ.φ   *)
282   | TCode          : RawHaskType -> RawHaskType                             -> RawHaskType   (* from λ^α *).
283 *)
284 Definition WellKindedHaskType Γ (ht:HaskType Γ) κ :=
285   forall TV env, WellKinded_RawHaskType TV Γ env (ht TV env) κ.
286   Notation "Γ '⊢ᴛy' σ : κ" := (WellKindedHaskType Γ σ κ).
287
288
289
290 (* "decl", production for "axiom" *)
291 Structure AxiomDecl (n:nat)(ccon:CoFunConst n)(vk:vec Kind n){TV:Type} : Type :=
292 { axd_τ : vec TV n -> RawHaskType TV
293 ; axd_σ : vec TV n -> RawHaskType TV
294 }.
295
296
297
298
299 Section WFCo.
300   Context {TV:Type}.
301   Context {CV:Type}.
302
303   (* local notations *)
304   Notation "ienv '⊢ᴛy' σ : κ"              := (@WellKinded_RawHaskType TV _ ienv σ κ).
305   Notation "env  ∋  cv : t1 ∼ t2 : Γ : t"  := (@coercionEnvContainsCoercion Γ _ TV CV t env cv (@mkRawCoercionKind _ t1 t2))
306                  (at level 20, t1 at level 99, t2 at level 99, t at level 99).
307   Reserved Notation "ice '⊢ᴄᴏ' γ : a '∼' b : Δ : Γ : ite"
308         (at level 20, γ at level 99, b at level 99, Δ at level 99, ite at level 99, Γ at level 99).
309
310   (* Figure 8, lower half *)
311   Inductive WFCoercion:forall Γ (Δ:CoercionEnv Γ),
312     @InstantiatedTypeEnv TV Γ ->
313     @InstantiatedCoercionEnv TV CV Γ Δ ->
314     @RawHaskCoer TV CV -> @RawCoercionKind TV -> Prop :=.
315 (*
316   | CoTVar':∀ Γ Δ t e c σ τ,
317     (@coercionEnvContainsCoercion Γ _ TV CV t e c (@mkRawCoercionKind _ σ τ)) -> e⊢ᴄᴏ CoVar c : σ ∼ τ  : Δ : Γ : t
318   | CoRefl :∀ Γ Δ t e   τ κ,                                         t ⊢ᴛy τ :κ    -> e⊢ᴄᴏ CoType τ    :         τ ∼ τ  : Δ :Γ: t
319   | Sym    :∀ Γ Δ t e γ σ τ,                            (e⊢ᴄᴏ γ : σ ∼ τ : Δ : Γ:t)  -> e⊢ᴄᴏ CoSym  γ    :         τ ∼ σ  : Δ :Γ: t
320   | Trans  :∀ Γ Δ t e γ₁ γ₂ σ₁ σ₂ σ₃,(e⊢ᴄᴏ γ₁:σ₁∼σ₂:Δ:Γ:t) -> (e⊢ᴄᴏ γ₂:σ₂∼σ₃:Δ:Γ:t) -> e⊢ᴄᴏ CoComp γ₁ γ₂:        σ₁ ∼ σ₃ : Δ :Γ: t
321   | Left   :∀ Γ Δ t e γ σ₁ σ₂ τ₁ τ₂,(e⊢ᴄᴏ γ : TApp σ₁ σ₂ ∼ TApp τ₁ τ₂ :Δ:Γ:t    )  -> e⊢ᴄᴏ CoLeft  γ   :        σ₁ ∼ τ₁ : Δ :Γ: t
322   | Right  :∀ Γ Δ t e γ σ₁ σ₂ τ₁ τ₂,(e⊢ᴄᴏ γ : TApp σ₁ σ₂ ∼ TApp τ₁ τ₂ :Δ:Γ:t   )   -> e⊢ᴄᴏ CoRight γ   :        σ₂ ∼ τ₂ : Δ :Γ: t
323   | SComp  :∀ Γ Δ t e γ n S σ τ κ,
324             ListWFCo Γ Δ t e γ σ τ -> t ⊢ᴛy TFCApp(n:=n) S σ : κ  -> e⊢ᴄᴏ CoTFApp S γ : TFCApp S σ∼TFCApp S τ : Δ : Γ : t
325   | CoAx   :∀ Γ Δ t e n C κ γ, forall (σ₁:vec TV n) (σ₂:vec TV n), forall (ax:@AxiomDecl n C κ TV),
326     ListWFCo                              Γ Δ t e γ (map TVar (vec2list σ₁)) (map TVar (vec2list σ₂)) ->
327     ListWellKinded_RawHaskType TV Γ t   (map TVar (vec2list σ₁))            (vec2list κ)  ->
328     ListWellKinded_RawHaskType TV Γ t   (map TVar (vec2list σ₂))            (vec2list κ)  ->
329     e⊢ᴄᴏ CoCFApp C γ : axd_σ _ _ _ ax σ₁ ∼ axd_τ _ _ _ ax σ₂ : Δ : Γ : t
330
331   | WFCoAll  : forall Γ Δ κ (t:InstantiatedTypeEnv TV Γ) (e:InstantiatedCoercionEnv TV CV (κ::Γ) (weakCE Δ)) γ σ τ    ,
332       (∀ a,           e ⊢ᴄᴏ (        γ a) : (       σ a) ∼ (       τ a) : _ : _ : (t + a : κ))
333       ->    weakICE e ⊢ᴄᴏ (CoAll κ γ  ) : (TAll κ σ  ) ∼ (TAll κ τ  ) : Δ : Γ :  t
334
335   | Comp   :forall Γ Δ t e γ₁ γ₂ σ₁ σ₂ τ₁ τ₂ κ,
336             (t ⊢ᴛy TApp σ₁ σ₂:κ)->
337             (e⊢ᴄᴏ γ₁:σ₁∼τ₁:Δ:Γ:t)->
338             (e⊢ᴄᴏ γ₂:σ₂∼τ₂:Δ:Γ:t) ->
339             e⊢ᴄᴏ (CoApp γ₁ γ₂) : (TApp σ₁ σ₂) ∼ (TApp τ₁ τ₂) : Δ:Γ:t
340
341   | CoInst :forall Γ Δ t e σ τ κ γ (v:∀ TV, InstantiatedTypeEnv TV Γ -> RawHaskType TV),
342           t ⊢ᴛy v TV t : κ  ->
343             (e⊢ᴄᴏ γ:HaskTAll κ σ _ t ∼ HaskTAll κ τ _ t:Δ:Γ:t) ->
344             e⊢ᴄᴏ CoAppT γ (v TV t) : substT σ v TV t ∼substT τ v TV t : Δ : Γ : t
345   with ListWFCo  : forall Γ (Δ:CoercionEnv Γ),
346      @InstantiatedTypeEnv TV Γ ->
347      InstantiatedCoercionEnv TV CV Γ Δ ->
348      list (RawHaskCoer TV CV) -> list (RawHaskType TV) -> list (RawHaskType TV) -> Prop :=
349   | LWFCo_nil  : ∀ Γ Δ t e ,                                                            ListWFCo Γ Δ t e nil     nil     nil
350   | LWFCo_cons : ∀ Γ Δ t e a b c la lb lc, (e⊢ᴄᴏ a : b∼c : Δ : Γ : t )->
351     ListWFCo Γ Δ t e la lb lc -> ListWFCo Γ Δ t e (a::la) (b::lb) (c::lc)
352   where "ice '⊢ᴄᴏ' γ : a '∼' b : Δ : Γ : ite" := (@WFCoercion Γ Δ ite ice γ (@mkRawCoercionKind _ a b)).
353 *)
354 End WFCo.
355
356 Definition WFCCo (Γ:TypeEnv)(Δ:CoercionEnv Γ)(γ:HaskCoercion Γ Δ)(a b:HaskType Γ) :=
357   forall {TV CV:Type}(env:@InstantiatedTypeEnv TV Γ)(cenv:InstantiatedCoercionEnv TV CV Γ Δ),
358     @WFCoercion _ _ Γ Δ env cenv (γ TV CV env cenv) (@mkRawCoercionKind _ (a TV env) (b TV env)).
359     Notation "Δ '⊢ᴄᴏ' γ : a '∼' b" := (@WFCCo _ Δ γ a b).
360
361   Definition literalType (lit:HaskLiteral){Γ} : HaskType Γ := fun TV ite => (TCon (haskLiteralToTyCon lit)).
362   Notation "a ∼∼∼ b" := (mkHaskCoercionKind a b) (at level 18).
363
364
365 Fixpoint update_ξ
366   `{EQD_VV:EqDecidable VV}{Γ}(ξ:VV -> LeveledHaskType Γ)(vt:list (VV * LeveledHaskType Γ)) : VV -> LeveledHaskType Γ :=
367   match vt with
368     | nil => ξ
369     | (v,τ)::tl => fun v' => if eqd_dec v v' then τ else (update_ξ ξ tl) v'
370   end.
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387 (* just like GHC's AltCon, but using PHOAS types and coercions *)
388 Record StrongCaseBranch {n}{tc:TyCon n}{Γ}{atypes:vec (HaskType Γ) n} :=
389 { scb_saci      :  @StrongAltConInfo _ tc
390 ; scb_Γ         :  TypeEnv := app (vec2list (saci_ekinds scb_saci)) Γ
391 ; scb_Δ         :  CoercionEnv scb_Γ
392 ; scb_types     :  vec (LeveledHaskType (app (vec2list (saci_ekinds scb_saci)) Γ)) (saci_numExprVars scb_saci)
393 }.
394 Coercion scb_saci : StrongCaseBranch >-> StrongAltConInfo.
395 Implicit Arguments StrongCaseBranch [[n][Γ]].
396