1 (*********************************************************************************************************************************)
4 (* The Flattening Functor. *)
6 (*********************************************************************************************************************************)
8 Generalizable All Variables.
9 Require Import Preamble.
10 Require Import General.
11 Require Import NaturalDeduction.
12 Require Import Coq.Strings.String.
13 Require Import Coq.Lists.List.
15 Require Import HaskKinds.
16 Require Import HaskCoreTypes.
17 Require Import HaskCoreVars.
18 Require Import HaskWeakTypes.
19 Require Import HaskWeakVars.
20 Require Import HaskLiteralsAndTyCons.
21 Require Import HaskStrongTypes.
22 Require Import HaskProof.
23 Require Import NaturalDeduction.
25 Require Import HaskStrongTypes.
26 Require Import HaskStrong.
27 Require Import HaskProof.
28 Require Import HaskStrongToProof.
29 Require Import HaskProofToStrong.
30 Require Import HaskWeakToStrong.
35 * The flattening transformation. Currently only TWO-level languages are
36 * supported, and the level-1 sublanguage is rather limited.
38 * This file abuses terminology pretty badly. For purposes of this file,
39 * "PCF" means "the level-1 sublanguage" and "FC" (aka System FC) means
40 * the whole language (level-0 language including bracketed level-1 terms)
42 Section HaskFlattener.
44 Inductive TreeFlags {T:Type} : Tree T -> Type :=
45 | tf_leaf_true : forall x, TreeFlags (T_Leaf x)
46 | tf_leaf_false : forall x, TreeFlags (T_Leaf x)
47 | tf_branch : forall b1 b2, TreeFlags b1 -> TreeFlags b2 -> TreeFlags (b1,,b2).
49 Fixpoint mkFlags {T}(f:T -> bool)(t:Tree T) : TreeFlags t :=
50 match t as T return TreeFlags T with
51 | T_Leaf x => if f x then tf_leaf_true x else tf_leaf_false x
52 | T_Branch b1 b2 => tf_branch _ _ (mkFlags f b1) (mkFlags f b2)
55 (* take and drop are not exact inverses *)
57 (* drop replaces each leaf where the flag is set with a [] *)
58 Fixpoint drop {T}{Σ:Tree ??T}(tf:TreeFlags Σ) : Tree ??T :=
60 | tf_leaf_true x => []
61 | tf_leaf_false x => Σ
62 | tf_branch b1 b2 tb1 tb2 => (drop tb1),,(drop tb2)
65 (* take returns only those leaves for which the flag is set; all others are omitted entirely from the tree *)
66 Fixpoint take {T}{Σ:Tree T}(tf:TreeFlags Σ) : ??(Tree T) :=
68 | tf_leaf_true x => Some Σ
69 | tf_leaf_false x => None
70 | tf_branch b1 b2 tb1 tb2 =>
73 | Some b1' => match take tb2 with
75 | Some b2' => Some (b1',,b2')
80 Definition maybeTree {T}(t:??(Tree ??T)) : Tree ??T :=
86 Definition setNone {T}(b:bool)(f:T -> bool) : ??T -> bool :=
93 (* "Arrange" objects are parametric in the type of the leaves of the tree *)
94 Definition arrangeMap :
95 forall {T} (Σ₁ Σ₂:Tree ??T) {R} (f:T -> R),
97 Arrange (mapOptionTree f Σ₁) (mapOptionTree f Σ₂).
111 eapply RComp; [ apply IHX1 | apply IHX2 ].
114 Definition bnot (b:bool) : bool := if b then false else true.
116 Definition swapMiddle {T} (a b c d:Tree ??T) :
117 Arrange ((a,,b),,(c,,d)) ((a,,c),,(b,,d)).
133 forall {T} (Σ:Tree ??T) (f:T -> bool),
134 Arrange Σ (drop (mkFlags (setNone false f) Σ),,( (drop (mkFlags (setNone false (bnot ○ f)) Σ)))).
140 destruct (f t); simpl.
146 eapply RComp; [ idtac | apply swapMiddle ].
154 Definition arrange' :
155 forall {T} (Σ:Tree ??T) (f:T -> bool),
156 Arrange (drop (mkFlags (setNone false f) Σ),,( (drop (mkFlags (setNone false (bnot ○ f)) Σ)))) Σ.
162 destruct (f t); simpl.
168 eapply RComp; [ apply swapMiddle | idtac ].
176 Definition minus' n m :=
185 Ltac eqd_dec_refl' :=
187 | [ |- context[@eqd_dec ?T ?V ?X ?X] ] =>
188 destruct (@eqd_dec T V X X) as [eqd_dec1 | eqd_dec2];
189 [ clear eqd_dec1 | set (eqd_dec2 (refl_equal _)) as eqd_dec2'; inversion eqd_dec2' ]
192 Fixpoint reduceTree {T}(unit:T)(merge:T -> T -> T)(tt:Tree ??T) : T :=
194 | T_Leaf None => unit
195 | T_Leaf (Some x) => x
196 | T_Branch b1 b2 => merge (reduceTree unit merge b1) (reduceTree unit merge b2)
199 Set Printing Width 130.
201 Context (hetmet_flatten : WeakExprVar).
202 Context (hetmet_unflatten : WeakExprVar).
203 Context (hetmet_id : WeakExprVar).
204 Context {unitTy : forall TV, RawHaskType TV ★ }.
205 Context {prodTy : forall TV, RawHaskType TV ★ -> RawHaskType TV ★ -> RawHaskType TV ★ }.
206 Context {gaTy : forall TV, RawHaskType TV ECKind -> RawHaskType TV ★ -> RawHaskType TV ★ -> RawHaskType TV ★ }.
208 Definition ga_mk_tree {Γ} (tr:Tree ??(HaskType Γ ★)) : HaskType Γ ★ :=
209 fun TV ite => reduceTree (unitTy TV) (prodTy TV) (mapOptionTree (fun x => x TV ite) tr).
211 Definition ga_mk {Γ}(ec:HaskType Γ ECKind )(ant suc:Tree ??(HaskType Γ ★)) : HaskType Γ ★ :=
212 fun TV ite => gaTy TV (ec TV ite) (ga_mk_tree ant TV ite) (ga_mk_tree suc TV ite).
215 { ga_id : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec a a @@ l] ]
216 ; ga_cancelr : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec (a,,[]) a @@ l] ]
217 ; ga_cancell : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec ([],,a) a @@ l] ]
218 ; ga_uncancelr : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec a (a,,[]) @@ l] ]
219 ; ga_uncancell : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec a ([],,a) @@ l] ]
220 ; ga_assoc : ∀ Γ Δ ec l a b c, ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec ((a,,b),,c) (a,,(b,,c)) @@ l] ]
221 ; ga_unassoc : ∀ Γ Δ ec l a b c, ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec (a,,(b,,c)) ((a,,b),,c) @@ l] ]
222 ; ga_swap : ∀ Γ Δ ec l a b , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec (a,,b) (b,,a) @@ l] ]
223 ; ga_drop : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec a [] @@ l] ]
224 ; ga_copy : ∀ Γ Δ ec l a , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec a (a,,a) @@ l] ]
225 ; ga_first : ∀ Γ Δ ec l a b x, ND Rule [] [Γ > Δ > [@ga_mk Γ ec a b @@ l] |- [@ga_mk Γ ec (a,,x) (b,,x) @@ l] ]
226 ; ga_second : ∀ Γ Δ ec l a b x, ND Rule [] [Γ > Δ > [@ga_mk Γ ec a b @@ l] |- [@ga_mk Γ ec (x,,a) (x,,b) @@ l] ]
227 ; ga_lit : ∀ Γ Δ ec l lit , ND Rule [] [Γ > Δ > [] |- [@ga_mk Γ ec [] [literalType lit] @@ l] ]
228 ; ga_curry : ∀ Γ Δ ec l a b c, ND Rule [] [Γ > Δ > [@ga_mk Γ ec (a,,[b]) [c] @@ l] |- [@ga_mk Γ ec a [b ---> c] @@ l] ]
229 ; ga_comp : ∀ Γ Δ ec l a b c, ND Rule [] [Γ > Δ > [@ga_mk Γ ec a b @@ l],,[@ga_mk Γ ec b c @@ l] |- [@ga_mk Γ ec a c @@ l] ]
230 ; ga_apply : ∀ Γ Δ ec l a a' b c, ND Rule []
231 [Γ > Δ > [@ga_mk Γ ec a [b ---> c] @@ l],,[@ga_mk Γ ec a' [b] @@ l] |- [@ga_mk Γ ec (a,,a') [c] @@ l] ]
232 ; ga_kappa : ∀ Γ Δ ec l a b Σ, ND Rule
233 [Γ > Δ > Σ,,[@ga_mk Γ ec [] a @@ l] |- [@ga_mk Γ ec [] b @@ l] ]
234 [Γ > Δ > Σ |- [@ga_mk Γ ec a b @@ l] ]
236 Context `(gar:garrow).
238 Notation "a ~~~~> b" := (@ga_mk _ _ a b) (at level 20).
242 * - code types <[t]>@c become garrows c () t
243 * - free variables of type t at a level lev deeper than the succedent become garrows c () t
244 * - free variables at the level of the succedent become
246 Fixpoint garrowfy_raw_codetypes {TV}{κ}(exp: RawHaskType TV κ) : RawHaskType TV κ :=
249 | TAll _ y => TAll _ (fun v => garrowfy_raw_codetypes (y v))
250 | TApp _ _ x y => TApp (garrowfy_raw_codetypes x) (garrowfy_raw_codetypes y)
252 | TCoerc _ t1 t2 t => TCoerc (garrowfy_raw_codetypes t1) (garrowfy_raw_codetypes t2)
253 (garrowfy_raw_codetypes t)
255 | TCode v e => gaTy TV v (unitTy TV) (garrowfy_raw_codetypes e)
256 | TyFunApp tfc kl k lt => TyFunApp tfc kl k (garrowfy_raw_codetypes_list _ lt)
258 with garrowfy_raw_codetypes_list {TV}(lk:list Kind)(exp:@RawHaskTypeList TV lk) : @RawHaskTypeList TV lk :=
259 match exp in @RawHaskTypeList _ LK return @RawHaskTypeList TV LK with
260 | TyFunApp_nil => TyFunApp_nil
261 | TyFunApp_cons κ kl t rest => TyFunApp_cons _ _ (garrowfy_raw_codetypes t) (garrowfy_raw_codetypes_list _ rest)
263 Definition garrowfy_code_types {Γ}{κ}(ht:HaskType Γ κ) : HaskType Γ κ :=
265 garrowfy_raw_codetypes (ht TV ite).
267 Definition v2t {Γ}(ec:HaskTyVar Γ ECKind) := fun TV ite => TVar (ec TV ite).
269 Fixpoint garrowfy_leveled_code_types' {Γ}(ht:HaskType Γ ★)(lev:HaskLevel Γ) : HaskType Γ ★ :=
271 | nil => garrowfy_code_types ht
272 | ec::lev' => @ga_mk _ (v2t ec) [] [garrowfy_leveled_code_types' ht lev']
275 Definition garrowfy_leveled_code_types {Γ}(ht:LeveledHaskType Γ ★) : LeveledHaskType Γ ★ :=
278 garrowfy_leveled_code_types' htt lev @@ nil
281 Definition levelMatch {Γ}(lev:HaskLevel Γ) : LeveledHaskType Γ ★ -> bool :=
282 fun t => match t with ttype@@tlev => if eqd_dec tlev lev then true else false end.
284 (* In a tree of types, replace any type at depth "lev" or greater None *)
285 Definition mkDropFlags {Γ}(lev:HaskLevel Γ)(tt:Tree ??(LeveledHaskType Γ ★)) : TreeFlags tt :=
286 mkFlags (setNone false (levelMatch lev)) tt.
288 Definition drop_lev {Γ}(lev:HaskLevel Γ)(tt:Tree ??(LeveledHaskType Γ ★)) : Tree ??(LeveledHaskType Γ ★) :=
289 drop (mkDropFlags lev tt).
291 (* The opposite: replace any type which is NOT at level "lev" with None *)
292 Definition mkTakeFlags {Γ}(lev:HaskLevel Γ)(tt:Tree ??(LeveledHaskType Γ ★)) : TreeFlags tt :=
293 mkFlags (setNone true (bnot ○ levelMatch lev)) tt.
295 Definition take_lev {Γ}(lev:HaskLevel Γ)(tt:Tree ??(LeveledHaskType Γ ★)) : Tree ??(HaskType Γ ★) :=
296 mapOptionTree (fun x => garrowfy_code_types (unlev x)) (drop (mkTakeFlags lev tt)).
298 mapOptionTree (fun x => garrowfy_code_types (unlev x))
299 (maybeTree (take tt (mkFlags (
300 fun t => match t with
301 | Some (ttype @@ tlev) => if eqd_dec tlev lev then true else false
307 Lemma drop_lev_lemma : forall Γ (lev:HaskLevel Γ) x, drop_lev lev [x @@ lev] = [].
319 Lemma drop_lev_lemma_s : forall Γ (lev:HaskLevel Γ) ec x, drop_lev (ec::lev) [x @@ (ec :: lev)] = [].
330 Lemma take_lemma : forall Γ (lev:HaskLevel Γ) x, take_lev lev [x @@ lev] = [garrowfy_code_types x].
341 Ltac drop_simplify :=
343 | [ |- context[@drop_lev ?G ?L [ ?X @@ ?L ] ] ] =>
344 rewrite (drop_lev_lemma G L X)
345 | [ |- context[@drop_lev ?G (?E :: ?L) [ ?X @@ (?E :: ?L) ] ] ] =>
346 rewrite (drop_lev_lemma_s G L E X)
347 | [ |- context[@drop_lev ?G ?N (?A,,?B)] ] =>
348 change (@drop_lev G N (A,,B)) with ((@drop_lev G N A),,(@drop_lev G N B))
349 | [ |- context[@drop_lev ?G ?N (T_Leaf None)] ] =>
350 change (@drop_lev G N (T_Leaf (@None (LeveledHaskType G ★)))) with (T_Leaf (@None (LeveledHaskType G ★)))
353 Ltac take_simplify :=
355 | [ |- context[@take_lev ?G ?L [ ?X @@ ?L ] ] ] =>
356 rewrite (take_lemma G L X)
357 | [ |- context[@take_lev ?G ?N (?A,,?B)] ] =>
358 change (@take_lev G N (A,,B)) with ((@take_lev G N A),,(@take_lev G N B))
359 | [ |- context[@take_lev ?G ?N (T_Leaf None)] ] =>
360 change (@take_lev G N (T_Leaf (@None (LeveledHaskType G ★)))) with (T_Leaf (@None (LeveledHaskType G ★)))
363 Axiom literal_types_unchanged : forall Γ l, garrowfy_code_types (literalType l) = literalType(Γ:=Γ) l.
365 Axiom flatten_coercion : forall Γ Δ κ (σ τ:HaskType Γ κ) (γ:HaskCoercion Γ Δ (σ ∼∼∼ τ)),
366 HaskCoercion Γ Δ (garrowfy_code_types σ ∼∼∼ garrowfy_code_types τ).
368 (* This tries to assign a single level to the entire succedent of a judgment. If the succedent has types from different
369 * levels (should not happen) it just picks one; if the succedent has no non-None leaves (also should not happen) it
371 Definition getΓ (j:Judg) := match j with Γ > _ > _ |- _ => Γ end.
372 Definition getSuc (j:Judg) : Tree ??(LeveledHaskType (getΓ j) ★) :=
373 match j as J return Tree ??(LeveledHaskType (getΓ J) ★) with Γ > _ > _ |- s => s end.
374 Fixpoint getjlev {Γ}(tt:Tree ??(LeveledHaskType Γ ★)) : HaskLevel Γ :=
377 | T_Leaf (Some (_ @@ lev)) => lev
379 match getjlev b1 with
385 Definition unlev' {Γ} (x:LeveledHaskType Γ ★) :=
386 garrowfy_code_types (unlev x).
388 (* "n" is the maximum depth remaining AFTER flattening *)
389 Definition flatten_judgment (j:Judg) :=
390 match j as J return Judg with
391 Γ > Δ > ant |- suc =>
392 match getjlev suc with
393 | nil => Γ > Δ > mapOptionTree garrowfy_leveled_code_types ant
394 |- mapOptionTree garrowfy_leveled_code_types suc
396 | (ec::lev') => Γ > Δ > mapOptionTree garrowfy_leveled_code_types (drop_lev (ec::lev') ant)
398 (take_lev (ec::lev') ant)
399 (mapOptionTree unlev' suc) (* we know the level of all of suc *)
404 Definition boost : forall Γ Δ ant x y {lev},
405 ND Rule [] [ Γ > Δ > [x@@lev] |- [y@@lev] ] ->
406 ND Rule [ Γ > Δ > ant |- [x@@lev] ] [ Γ > Δ > ant |- [y@@lev] ].
408 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanL ].
409 eapply nd_comp; [ idtac | eapply nd_rule; apply (@RLet Γ Δ [] ant y x lev) ].
410 eapply nd_comp; [ apply nd_rlecnac | idtac ].
420 Definition postcompose' : ∀ Γ Δ ec lev a b c Σ,
421 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec a b @@ lev] ] ->
422 ND Rule [] [ Γ > Δ > Σ,,[@ga_mk Γ ec b c @@ lev] |- [@ga_mk Γ ec a c @@ lev] ].
424 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RExch ].
425 eapply nd_comp; [ idtac | eapply nd_rule; apply (@RLet Γ Δ [@ga_mk _ ec b c @@lev] Σ (@ga_mk _ ec a c) (@ga_mk _ ec a b) lev) ].
426 eapply nd_comp; [ apply nd_llecnac | idtac ].
429 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; apply RExch ].
433 Definition precompose' : ∀ Γ Δ ec lev a b c Σ,
434 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec b c @@ lev] ] ->
435 ND Rule [] [ Γ > Δ > Σ,,[@ga_mk Γ ec a b @@ lev] |- [@ga_mk Γ ec a c @@ lev] ].
437 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RExch ].
438 eapply nd_comp; [ idtac | eapply nd_rule; apply (@RLet Γ Δ [@ga_mk _ ec a b @@lev] Σ (@ga_mk _ ec a c) (@ga_mk _ ec b c) lev) ].
439 eapply nd_comp; [ apply nd_llecnac | idtac ].
445 Definition postcompose : ∀ Γ Δ ec lev a b c,
446 ND Rule [] [ Γ > Δ > [] |- [@ga_mk Γ ec a b @@ lev] ] ->
447 ND Rule [] [ Γ > Δ > [@ga_mk Γ ec b c @@ lev] |- [@ga_mk Γ ec a c @@ lev] ].
457 Definition firstify : ∀ Γ Δ ec lev a b c Σ,
458 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec a b @@ lev] ] ->
459 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec (a,,c) (b,,c) @@ lev] ].
461 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanL ].
462 eapply nd_comp; [ idtac | eapply nd_rule; apply RLet ].
463 eapply nd_comp; [ apply nd_llecnac | idtac ].
466 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RuCanL ].
470 Definition secondify : ∀ Γ Δ ec lev a b c Σ,
471 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec a b @@ lev] ] ->
472 ND Rule [] [ Γ > Δ > Σ |- [@ga_mk Γ ec (c,,a) (c,,b) @@ lev] ].
474 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanL ].
475 eapply nd_comp; [ idtac | eapply nd_rule; apply RLet ].
476 eapply nd_comp; [ apply nd_llecnac | idtac ].
479 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RuCanL ].
483 Lemma ga_unkappa : ∀ Γ Δ ec l a b Σ,
485 [Γ > Δ > Σ |- [@ga_mk Γ ec a b @@ l] ]
486 [Γ > Δ > Σ,,[@ga_mk Γ ec [] a @@ l] |- [@ga_mk Γ ec [] b @@ l] ].
488 set (ga_comp Γ Δ ec l [] a b) as q.
490 set (@RLet Γ Δ) as q'.
491 set (@RLet Γ Δ [@ga_mk _ ec [] a @@ l] Σ (@ga_mk _ ec [] b) (@ga_mk _ ec a b) l) as q''.
513 Notation "` x" := (take_lev _ x) (at level 20).
514 Notation "`` x" := (mapOptionTree unlev x) (at level 20).
515 Notation "``` x" := (drop_lev _ x) (at level 20).
517 Definition garrowfy_arrangement' :
518 forall Γ (Δ:CoercionEnv Γ)
519 (ec:HaskTyVar Γ ECKind) (lev:HaskLevel Γ) (ant1 ant2:Tree ??(LeveledHaskType Γ ★)) (r:Arrange ant1 ant2),
520 ND Rule [] [Γ > Δ > [] |- [@ga_mk _ (v2t ec) (take_lev (ec :: lev) ant2) (take_lev (ec :: lev) ant1) @@ nil] ].
523 refine (fix garrowfy ant1 ant2 (r:Arrange ant1 ant2):
524 ND Rule [] [Γ > Δ > [] |- [@ga_mk _ (v2t ec) (take_lev (ec :: lev) ant2) (take_lev (ec :: lev) ant1) @@ nil]] :=
525 match r as R in Arrange A B return
526 ND Rule [] [Γ > Δ > [] |- [@ga_mk _ (v2t ec) (take_lev (ec :: lev) B) (take_lev (ec :: lev) A) @@ nil]]
528 | RCanL a => let case_RCanL := tt in ga_uncancell _ _ _ _ _
529 | RCanR a => let case_RCanR := tt in ga_uncancelr _ _ _ _ _
530 | RuCanL a => let case_RuCanL := tt in ga_cancell _ _ _ _ _
531 | RuCanR a => let case_RuCanR := tt in ga_cancelr _ _ _ _ _
532 | RAssoc a b c => let case_RAssoc := tt in ga_assoc _ _ _ _ _ _ _
533 | RCossa a b c => let case_RCossa := tt in ga_unassoc _ _ _ _ _ _ _
534 | RExch a b => let case_RExch := tt in ga_swap _ _ _ _ _ _
535 | RWeak a => let case_RWeak := tt in ga_drop _ _ _ _ _
536 | RCont a => let case_RCont := tt in ga_copy _ _ _ _ _
537 | RLeft a b c r' => let case_RLeft := tt in garrowfy _ _ r' ;; boost _ _ _ _ _ (ga_second _ _ _ _ _ _ _)
538 | RRight a b c r' => let case_RRight := tt in garrowfy _ _ r' ;; boost _ _ _ _ _ (ga_first _ _ _ _ _ _ _)
539 | RComp c b a r1 r2 => let case_RComp := tt in (fun r1' r2' => _) (garrowfy _ _ r1) (garrowfy _ _ r2)
540 end); clear garrowfy; repeat take_simplify; repeat drop_simplify; intros.
543 set (take_lev (ec :: lev) a) as a' in *.
544 set (take_lev (ec :: lev) b) as b' in *.
545 set (take_lev (ec :: lev) c) as c' in *.
546 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; apply RCanL ].
547 eapply nd_comp; [ idtac | eapply nd_rule; apply
548 (@RLet Γ Δ [] [] (@ga_mk _ (v2t ec) a' c') (@ga_mk _ (v2t ec) a' b')) ].
549 eapply nd_comp; [ apply nd_llecnac | idtac ].
552 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; apply RuCanL ].
553 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; apply RCanR ].
554 eapply nd_comp; [ idtac | eapply nd_rule; apply
555 (@RLet Γ Δ [@ga_mk _ (v2t ec) a' b' @@ _] [] (@ga_mk _ (v2t ec) a' c') (@ga_mk _ (v2t ec) b' c'))].
556 eapply nd_comp; [ apply nd_llecnac | idtac ].
562 Definition garrowfy_arrangement :
563 forall Γ (Δ:CoercionEnv Γ) n
564 (ec:HaskTyVar Γ ECKind) (lev:HaskLevel Γ) (ant1 ant2:Tree ??(LeveledHaskType Γ ★)) (r:Arrange ant1 ant2) succ,
566 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) (drop_lev n ant1)
567 |- [@ga_mk _ (v2t ec) (take_lev (ec :: lev) ant1) (mapOptionTree (unlev' ) succ) @@ nil]]
568 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) (drop_lev n ant2)
569 |- [@ga_mk _ (v2t ec) (take_lev (ec :: lev) ant2) (mapOptionTree (unlev' ) succ) @@ nil]].
571 refine ( _ ;; (boost _ _ _ _ _ (postcompose _ _ _ _ _ _ _ (garrowfy_arrangement' Γ Δ ec lev ant1 ant2 r)))).
574 refine ((fix garrowfy ant1 ant2 (r:Arrange ant1 ant2) :=
575 match r as R in Arrange A B return
576 Arrange (mapOptionTree (garrowfy_leveled_code_types ) (drop_lev _ A))
577 (mapOptionTree (garrowfy_leveled_code_types ) (drop_lev _ B)) with
578 | RCanL a => let case_RCanL := tt in RCanL _
579 | RCanR a => let case_RCanR := tt in RCanR _
580 | RuCanL a => let case_RuCanL := tt in RuCanL _
581 | RuCanR a => let case_RuCanR := tt in RuCanR _
582 | RAssoc a b c => let case_RAssoc := tt in RAssoc _ _ _
583 | RCossa a b c => let case_RCossa := tt in RCossa _ _ _
584 | RExch a b => let case_RExch := tt in RExch _ _
585 | RWeak a => let case_RWeak := tt in RWeak _
586 | RCont a => let case_RCont := tt in RCont _
587 | RLeft a b c r' => let case_RLeft := tt in RLeft _ (garrowfy _ _ r')
588 | RRight a b c r' => let case_RRight := tt in RRight _ (garrowfy _ _ r')
589 | RComp a b c r1 r2 => let case_RComp := tt in RComp (garrowfy _ _ r1) (garrowfy _ _ r2)
590 end) ant1 ant2 r); clear garrowfy; repeat take_simplify; repeat drop_simplify; intros.
593 Definition flatten_arrangement :
594 forall Γ Δ ant1 ant2 succ (r:Arrange ant1 ant2),
595 ND Rule (mapOptionTree (flatten_judgment ) [Γ > Δ > ant1 |- succ])
596 (mapOptionTree (flatten_judgment ) [Γ > Δ > ant2 |- succ]).
599 set (getjlev succ) as succ_lev.
600 assert (succ_lev=getjlev succ).
618 eapply RComp; [ apply IHr1 | apply IHr2 ].
620 apply garrowfy_arrangement.
624 Definition ga_join Γ Δ Σ₁ Σ₂ a b ec :
625 ND Rule [] [Γ > Δ > Σ₁ |- [@ga_mk _ ec [] a @@ nil]] ->
626 ND Rule [] [Γ > Δ > Σ₂ |- [@ga_mk _ ec [] b @@ nil]] ->
627 ND Rule [] [Γ > Δ > Σ₁,,Σ₂ |- [@ga_mk _ ec [] (a,,b) @@ nil]].
630 apply secondify with (c:=a) in pfb.
633 eapply nd_comp; [ idtac | eapply nd_rule; eapply RLet ].
634 eapply nd_comp; [ eapply nd_llecnac | idtac ].
643 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanL ].
649 Definition arrange_brak : forall Γ Δ ec succ t,
651 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) (drop_lev (ec :: nil) succ),,
652 [(@ga_mk _ (v2t ec) [] (take_lev (ec :: nil) succ)) @@ nil] |- [(@ga_mk _ (v2t ec) [] [garrowfy_code_types t]) @@ nil]]
653 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) succ |- [(@ga_mk _ (v2t ec) [] [garrowfy_code_types t]) @@ nil]].
656 set (@arrange' _ succ (levelMatch (ec::nil))) as q.
657 set (arrangeMap _ _ garrowfy_leveled_code_types q) as y.
666 eapply nd_comp; [ apply nd_llecnac | idtac ].
667 eapply nd_comp; [ idtac | eapply nd_rule; eapply RLet ].
672 induction succ; try destruct a; simpl.
678 destruct l as [t' lev'].
679 destruct lev' as [|ec' lev'].
683 set (@eqd_dec (HaskLevel Γ) (haskLevelEqDecidable Γ) (ec' :: lev') (ec :: nil)) as q.
699 Definition arrange_esc : forall Γ Δ ec succ t,
701 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) succ |- [(@ga_mk _ (v2t ec) [] [garrowfy_code_types t]) @@ nil]]
702 [Γ > Δ > mapOptionTree (garrowfy_leveled_code_types ) (drop_lev (ec :: nil) succ),,
703 [(@ga_mk _ (v2t ec) [] (take_lev (ec :: nil) succ)) @@ nil] |- [(@ga_mk _ (v2t ec) [] [garrowfy_code_types t]) @@ nil]].
706 set (@arrange _ succ (levelMatch (ec::nil))) as q.
707 set (arrangeMap _ _ garrowfy_leveled_code_types q) as y.
719 unfold mkDropFlags; simpl.
723 destruct (General.list_eq_dec h0 (ec :: nil)).
725 unfold garrowfy_leveled_code_types'.
740 apply (Prelude_error "escapifying code with multi-leaf antecedents is not supported").
743 Lemma mapOptionTree_distributes
744 : forall T R (a b:Tree ??T) (f:T->R),
745 mapOptionTree f (a,,b) = (mapOptionTree f a),,(mapOptionTree f b).
749 Axiom garrowfy_commutes_with_substT :
750 forall κ Γ (Δ:CoercionEnv Γ) (σ:∀ TV, InstantiatedTypeEnv TV Γ → TV κ → RawHaskType TV ★) (τ:HaskType Γ κ),
751 garrowfy_code_types (substT σ τ) = substT (fun TV ite v => garrowfy_raw_codetypes (σ TV ite v))
752 (garrowfy_code_types τ).
754 Axiom garrowfy_commutes_with_HaskTAll :
755 forall κ Γ (Δ:CoercionEnv Γ) (σ:∀ TV, InstantiatedTypeEnv TV Γ → TV κ → RawHaskType TV ★),
756 garrowfy_code_types (HaskTAll κ σ) = HaskTAll κ (fun TV ite v => garrowfy_raw_codetypes (σ TV ite v)).
758 Axiom garrowfy_commutes_with_HaskTApp :
759 forall κ Γ (Δ:CoercionEnv Γ) (σ:∀ TV, InstantiatedTypeEnv TV Γ → TV κ → RawHaskType TV ★),
760 garrowfy_code_types (HaskTApp (weakF σ) (FreshHaskTyVar κ)) =
761 HaskTApp (weakF (fun TV ite v => garrowfy_raw_codetypes (σ TV ite v))) (FreshHaskTyVar κ).
763 Axiom garrowfy_commutes_with_weakLT : forall (Γ:TypeEnv) κ t,
764 garrowfy_leveled_code_types (weakLT(Γ:=Γ)(κ:=κ) t) = weakLT(Γ:=Γ)(κ:=κ) (garrowfy_leveled_code_types t).
766 Axiom globals_do_not_have_code_types : forall (Γ:TypeEnv) (g:Global Γ) v,
767 garrowfy_code_types (g v) = g v.
769 Definition decide_tree_empty : forall {T:Type}(t:Tree ??T),
770 sum { q:Tree unit & t = mapTree (fun _ => None) q } unit.
775 | T_Branch b1 b2 => let b1' := foo b1 in let b2' := foo b2 in _
796 Definition flatten_proof :
799 ND Rule (mapOptionTree (flatten_judgment ) h) (mapOptionTree (flatten_judgment ) c).
801 eapply nd_map'; [ idtac | apply X ].
806 refine (match X as R in Rule H C with
807 | RArrange Γ Δ a b x d => let case_RArrange := tt in _
808 | RNote Γ Δ Σ τ l n => let case_RNote := tt in _
809 | RLit Γ Δ l _ => let case_RLit := tt in _
810 | RVar Γ Δ σ lev => let case_RVar := tt in _
811 | RGlobal Γ Δ σ l wev => let case_RGlobal := tt in _
812 | RLam Γ Δ Σ tx te lev => let case_RLam := tt in _
813 | RCast Γ Δ Σ σ τ lev γ => let case_RCast := tt in _
814 | RAbsT Γ Δ Σ κ σ lev => let case_RAbsT := tt in _
815 | RAppT Γ Δ Σ κ σ τ lev => let case_RAppT := tt in _
816 | RAppCo Γ Δ Σ κ σ₁ σ₂ γ σ lev => let case_RAppCo := tt in _
817 | RAbsCo Γ Δ Σ κ σ σ₁ σ₂ lev => let case_RAbsCo := tt in _
818 | RApp Γ Δ Σ₁ Σ₂ tx te lev => let case_RApp := tt in _
819 | RLet Γ Δ Σ₁ Σ₂ σ₁ σ₂ lev => let case_RLet := tt in _
820 | RJoin Γ p lri m x q => let case_RJoin := tt in _
821 | RVoid _ _ => let case_RVoid := tt in _
822 | RBrak Γ Δ t ec succ lev => let case_RBrak := tt in _
823 | REsc Γ Δ t ec succ lev => let case_REsc := tt in _
824 | RCase Γ Δ lev tc Σ avars tbranches alts => let case_RCase := tt in _
825 | RLetRec Γ Δ lri x y t => let case_RLetRec := tt in _
828 destruct case_RArrange.
829 apply (flatten_arrangement Γ Δ a b x d).
834 change ([garrowfy_code_types (<[ ec |- t ]>) @@ nil])
835 with ([ga_mk (v2t ec) [] [garrowfy_code_types t] @@ nil]).
836 refine (ga_unkappa Γ Δ (v2t ec) nil (take_lev (ec::nil) succ) _
837 (mapOptionTree (garrowfy_leveled_code_types) (drop_lev (ec::nil) succ)) ;; _ ).
839 apply (Prelude_error "found Brak at depth >0 (a)").
845 change ([garrowfy_code_types (<[ ec |- t ]>) @@ nil])
846 with ([ga_mk (v2t ec) [] [garrowfy_code_types t] @@ nil]).
847 eapply nd_comp; [ apply arrange_esc | idtac ].
848 set (decide_tree_empty (take_lev (ec :: nil) succ)) as q'.
854 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanR ].
855 eapply nd_comp; [ idtac | eapply nd_rule; eapply RLet ].
856 eapply nd_comp; [ apply nd_llecnac | idtac ].
857 apply nd_prod; [ idtac | eapply boost ].
860 eapply nd_comp; [ idtac | eapply nd_rule; eapply RArrange; eapply RCanR ].
869 (* environment has non-empty leaves *)
870 apply (ga_kappa Γ Δ (v2t ec) nil (take_lev (ec::nil) succ) _ _).
871 apply (Prelude_error "found Esc at depth >0 (a)").
876 apply nd_rule; apply RNote; auto.
877 apply nd_rule; apply RNote; auto.
882 rewrite literal_types_unchanged.
883 apply nd_rule; apply RLit.
884 unfold take_lev; simpl.
885 unfold drop_lev; simpl.
888 rewrite literal_types_unchanged.
892 Opaque flatten_judgment.
894 Transparent flatten_judgment.
896 unfold flatten_judgment.
899 apply nd_rule. apply RVar.
900 repeat drop_simplify.
902 repeat take_simplify.
906 destruct case_RGlobal.
910 destruct l as [|ec lev]; simpl.
911 destruct (eqd_dec (g:CoreVar) (hetmet_flatten:CoreVar)).
912 set (garrowfy_code_types (g wev)) as t.
913 set (RGlobal _ Δ nil (mkGlobal Γ t hetmet_id)) as q.
918 destruct (eqd_dec (g:CoreVar) (hetmet_unflatten:CoreVar)).
919 set (garrowfy_code_types (g wev)) as t.
920 set (RGlobal _ Δ nil (mkGlobal Γ t hetmet_id)) as q.
925 apply nd_rule; rewrite globals_do_not_have_code_types.
927 apply (Prelude_error "found RGlobal at depth >0").
933 destruct lev as [|ec lev]; simpl; [ apply nd_rule; apply RLam; auto | idtac ].
934 repeat drop_simplify.
935 repeat take_simplify.
946 destruct lev as [|ec lev]; simpl; [ apply nd_rule; apply RCast; auto | idtac ].
947 apply flatten_coercion; auto.
948 apply (Prelude_error "RCast at level >0").
952 destruct (getjlev x); destruct (getjlev q).
955 apply (Prelude_error "RJoin at depth >0").
956 apply (Prelude_error "RJoin at depth >0").
957 apply (Prelude_error "RJoin at depth >0").
962 destruct lev as [|ec lev]. simpl. apply nd_rule.
963 replace (garrowfy_code_types (tx ---> te)) with ((garrowfy_code_types tx) ---> (garrowfy_code_types te)).
967 repeat drop_simplify.
968 repeat take_simplify.
969 rewrite mapOptionTree_distributes.
970 set (mapOptionTree (garrowfy_leveled_code_types ) (drop_lev (ec :: lev) Σ₁)) as Σ₁'.
971 set (mapOptionTree (garrowfy_leveled_code_types ) (drop_lev (ec :: lev) Σ₂)) as Σ₂'.
972 set (take_lev (ec :: lev) Σ₁) as Σ₁''.
973 set (take_lev (ec :: lev) Σ₂) as Σ₂''.
974 replace (garrowfy_code_types (tx ---> te)) with ((garrowfy_code_types tx) ---> (garrowfy_code_types te)).
975 apply (Prelude_error "FIXME: ga_apply").
978 Notation "` x" := (take_lev _ x) (at level 20).
979 Notation "`` x" := (mapOptionTree unlev x) (at level 20).
980 Notation "``` x" := ((drop_lev _ x)) (at level 20).
981 Notation "!<[]> x" := (garrowfy_code_types _ x) (at level 1).
982 Notation "!<[@]>" := (garrowfy_leveled_code_types _) (at level 1).
985 apply (Prelude_error "FIXME: RLet").
988 destruct lev as [|ec lev]; simpl; [ apply nd_rule; apply RLet; auto | idtac ].
989 destruct (Peano_dec.eq_nat_dec (Datatypes.length lev) n); [ idtac | apply nd_rule; apply RLet; auto ]; simpl.
990 repeat drop_simplify.
991 repeat take_simplify.
994 rewrite mapOptionTree_distributes.
995 rewrite mapOptionTree_distributes.
996 set (mapOptionTree (garrowfy_leveled_code_types (S n)) (drop_lev (ec :: lev) Σ₁)) as A.
997 set (take_lev (ec :: lev) Σ₁) as A'.
998 set (mapOptionTree (garrowfy_leveled_code_types (S n)) (drop_lev (ec :: lev) Σ₂)) as B.
999 set (take_lev (ec :: lev) Σ₂) as B'.
1042 destruct case_RVoid.
1047 destruct case_RAppT.
1048 simpl. destruct lev; simpl.
1049 rewrite garrowfy_commutes_with_HaskTAll.
1050 rewrite garrowfy_commutes_with_substT.
1055 apply (Prelude_error "ga_apply").
1057 destruct case_RAbsT.
1058 simpl. destruct lev; simpl.
1059 rewrite garrowfy_commutes_with_HaskTAll.
1060 rewrite garrowfy_commutes_with_HaskTApp.
1061 eapply nd_comp; [ idtac | eapply nd_rule; eapply RAbsT ].
1063 set (mapOptionTree (garrowfy_leveled_code_types ) (mapOptionTree (weakLT(κ:=κ)) Σ)) as a.
1064 set (mapOptionTree (weakLT(κ:=κ)) (mapOptionTree (garrowfy_leveled_code_types ) Σ)) as q'.
1072 rewrite garrowfy_commutes_with_weakLT.
1083 apply (Prelude_error "AbsT at depth>0").
1085 destruct case_RAppCo.
1086 simpl. destruct lev; simpl.
1087 unfold garrowfy_code_types.
1091 apply flatten_coercion.
1093 apply (Prelude_error "AppCo at depth>0").
1095 destruct case_RAbsCo.
1096 simpl. destruct lev; simpl.
1097 unfold garrowfy_code_types.
1099 apply (Prelude_error "AbsCo not supported (FIXME)").
1100 apply (Prelude_error "AbsCo at depth>0").
1102 destruct case_RLetRec.
1104 apply (Prelude_error "LetRec not supported (FIXME)").
1106 destruct case_RCase.
1108 apply (Prelude_error "Case not supported (FIXME)").
1112 (* to do: establish some metric on judgments (max length of level of any succedent type, probably), show how to
1113 * calculate it, and show that the flattening procedure above drives it down by one *)
1116 Instance FlatteningFunctor {Γ}{Δ}{ec} : Functor (JudgmentsL (PCF Γ Δ ec)) (TypesL (SystemFCa Γ Δ)) (obact) :=
1117 { fmor := FlatteningFunctor_fmor }.
1119 Definition ReificationFunctor Γ Δ : Functor (JudgmentsL _ _ (PCF n Γ Δ)) SystemFCa' (mapOptionTree brakifyJudg).
1120 refine {| fmor := ReificationFunctor_fmor Γ Δ |}; unfold hom; unfold ob; simpl ; intros.
1122 Definition PCF_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
1123 refine {| plsmme_pl := PCF n Γ Δ |}.
1126 Definition SystemFCa_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
1127 refine {| plsmme_pl := SystemFCa n Γ Δ |}.
1130 Definition ReificationFunctorMonoidal n : MonoidalFunctor (JudgmentsN n) (JudgmentsN (S n)) (ReificationFunctor n).
1134 Definition PCF_SystemFCa_two_level n Γ Δ : TwoLevelLanguage (PCF_SMME n Γ Δ) (SystemFCa_SMME (S n) Γ Δ).
1137 (* ... and the retraction exists *)
1141 Implicit Arguments garrow [ ].