+ Context (ndr_systemfc:@ND_Relation _ Rule).
+
+ Inductive PCFJudg Γ (Δ:CoercionEnv Γ) (ec:HaskTyVar Γ ★) :=
+ pcfjudg : Tree ??(HaskType Γ ★) -> Tree ??(HaskType Γ ★) -> PCFJudg Γ Δ ec.
+ Implicit Arguments pcfjudg [ [Γ] [Δ] [ec] ].
+
+ (* given an PCFJudg at depth (ec::depth) we can turn it into an PCFJudg
+ * from depth (depth) by wrapping brackets around everything in the
+ * succedent and repopulating *)
+ Definition brakify {Γ}{Δ}{ec} (j:PCFJudg Γ Δ ec) : Judg :=
+ match j with
+ pcfjudg Σ τ => Γ > Δ > (Σ@@@(ec::nil)) |- (mapOptionTree (fun t => HaskBrak ec t) τ @@@ nil)
+ end.
+
+ Definition pcf_vars {Γ}(ec:HaskTyVar Γ ★)(t:Tree ??(LeveledHaskType Γ ★)) : Tree ??(HaskType Γ ★)
+ := mapOptionTreeAndFlatten (fun lt =>
+ match lt with t @@ l => match l with
+ | ec'::nil => if eqd_dec ec ec' then [t] else []
+ | _ => []
+ end
+ end) t.
+
+ Inductive MatchingJudgments {Γ}{Δ}{ec} : Tree ??(PCFJudg Γ Δ ec) -> Tree ??Judg -> Type :=
+ | match_nil : MatchingJudgments [] []
+ | match_branch : forall a b c d, MatchingJudgments a b -> MatchingJudgments c d -> MatchingJudgments (a,,c) (b,,d)
+ | match_leaf :
+ forall Σ τ lev,
+ MatchingJudgments
+ [pcfjudg (pcf_vars ec Σ) τ ]
+ [Γ > Δ > Σ |- (mapOptionTree (HaskBrak ec) τ @@@ lev)].
+
+ Definition fc_vars {Γ}(ec:HaskTyVar Γ ★)(t:Tree ??(LeveledHaskType Γ ★)) : Tree ??(HaskType Γ ★)
+ := mapOptionTreeAndFlatten (fun lt =>
+ match lt with t @@ l => match l with
+ | ec'::nil => if eqd_dec ec ec' then [] else [t]
+ | _ => []
+ end
+ end) t.
+
+ Definition pcfjudg2judg {Γ}{Δ:CoercionEnv Γ} ec (cj:PCFJudg Γ Δ ec) :=
+ match cj with pcfjudg Σ τ => Γ > Δ > (Σ @@@ (ec::nil)) |- (τ @@@ (ec::nil)) end.
+
+ (* Rules allowed in PCF; i.e. rules we know how to turn into GArrows *)
+ (* Rule_PCF consists of the rules allowed in flat PCF: everything except *)
+ (* AppT, AbsT, AppC, AbsC, Cast, Global, and some Case statements *)
+ Inductive Rule_PCF {Γ}{Δ:CoercionEnv Γ} (ec:HaskTyVar Γ ★)
+ : forall (h c:Tree ??(PCFJudg Γ Δ ec)), Rule (mapOptionTree (pcfjudg2judg ec) h) (mapOptionTree (pcfjudg2judg ec) c) -> Type :=
+ | PCF_RArrange : ∀ x y t a, Rule_PCF ec [pcfjudg _ _ ] [ pcfjudg _ _ ] (RArrange Γ Δ (x@@@(ec::nil)) (y@@@(ec::nil)) (t@@@(ec::nil)) a)
+ | PCF_RLit : ∀ lit , Rule_PCF ec [ ] [ pcfjudg [] [_] ] (RLit Γ Δ lit (ec::nil))
+ | PCF_RNote : ∀ Σ τ n , Rule_PCF ec [pcfjudg _ [_]] [ pcfjudg _ [_] ] (RNote Γ Δ (Σ@@@(ec::nil)) τ (ec::nil) n)
+ | PCF_RVar : ∀ σ , Rule_PCF ec [ ] [ pcfjudg [_] [_] ] (RVar Γ Δ σ (ec::nil) )
+ | PCF_RLam : ∀ Σ tx te , Rule_PCF ec [pcfjudg (_,,[_]) [_] ] [ pcfjudg _ [_] ] (RLam Γ Δ (Σ@@@(ec::nil)) tx te (ec::nil) )
+
+ | PCF_RApp : ∀ Σ Σ' tx te ,
+ Rule_PCF ec ([pcfjudg _ [_]],,[pcfjudg _ [_]]) [pcfjudg (_,,_) [_]]
+ (RApp Γ Δ (Σ@@@(ec::nil))(Σ'@@@(ec::nil)) tx te (ec::nil))
+
+ | PCF_RLet : ∀ Σ Σ' σ₂ p,
+ Rule_PCF ec ([pcfjudg _ [_]],,[pcfjudg (_,,[_]) [_]]) [pcfjudg (_,,_) [_]]
+ (RLet Γ Δ (Σ@@@(ec::nil)) (Σ'@@@(ec::nil)) σ₂ p (ec::nil))
+
+ | PCF_REmptyGroup : Rule_PCF ec [ ] [ pcfjudg [] [] ] (REmptyGroup Γ Δ )
+(*| PCF_RLetRec : ∀ Σ₁ τ₁ τ₂ , Rule_PCF (ec::nil) _ _ (RLetRec Γ Δ Σ₁ τ₁ τ₂ (ec::nil) )*)
+ | PCF_RBindingGroup : ∀ Σ₁ Σ₂ τ₁ τ₂, Rule_PCF ec ([pcfjudg _ _],,[pcfjudg _ _]) [pcfjudg (_,,_) (_,,_)]
+ (RBindingGroup Γ Δ (Σ₁@@@(ec::nil)) (Σ₂@@@(ec::nil)) (τ₁@@@(ec::nil)) (τ₂@@@(ec::nil))).
+ (* need int/boolean case *)
+ Implicit Arguments Rule_PCF [ ].
+
+ Definition PCFRule Γ Δ lev h c := { r:_ & @Rule_PCF Γ Δ lev h c r }.
+
+ (* An organized deduction has been reorganized into contiguous blocks whose
+ * hypotheses (if any) and conclusion have the same Γ and Δ and a fixed nesting depth. The boolean
+ * indicates if non-PCF rules have been used *)
+ Inductive OrgR : Tree ??Judg -> Tree ??Judg -> Type :=
+
+ | org_fc : forall h c (r:Rule h c),
+ Rule_Flat r ->
+ OrgR h c
+
+ | org_pcf : forall Γ Δ ec h h' c c',
+ MatchingJudgments h h' ->
+ MatchingJudgments c c' ->
+ ND (PCFRule Γ Δ ec) h c ->
+ OrgR h' c'.
+
+ Definition mkEsc {Γ}{Δ}{ec}(h:Tree ??(PCFJudg Γ Δ ec))
+ : ND Rule
+ (mapOptionTree brakify h)
+ (mapOptionTree (pcfjudg2judg ec) h).
+ apply nd_replicate; intros.
+ destruct o; simpl in *.
+ induction t0.
+ destruct a; simpl.
+ apply nd_rule.
+ apply REsc.
+ apply nd_id.
+ apply (Prelude_error "mkEsc got multi-leaf succedent").
+ Defined.
+
+ Definition mkBrak {Γ}{Δ}{ec}(h:Tree ??(PCFJudg Γ Δ ec))
+ : ND Rule
+ (mapOptionTree (pcfjudg2judg ec) h)
+ (mapOptionTree brakify h).
+ apply nd_replicate; intros.
+ destruct o; simpl in *.
+ induction t0.
+ destruct a; simpl.
+ apply nd_rule.
+ apply RBrak.
+ apply nd_id.
+ apply (Prelude_error "mkBrak got multi-leaf succedent").
+ Defined.
+
+ (*
+ Definition Partition {Γ} ec (Σ:Tree ??(LeveledHaskType Γ ★)) :=
+ { vars:(_ * _) |
+ fc_vars ec Σ = fst vars /\
+ pcf_vars ec Σ = snd vars }.
+ *)
+
+ Definition pcfToND : forall Γ Δ ec h c,
+ ND (PCFRule Γ Δ ec) h c -> ND Rule (mapOptionTree (pcfjudg2judg ec) h) (mapOptionTree (pcfjudg2judg ec) c).
+ intros.
+ eapply (fun q => nd_map' _ q X).
+ intros.
+ destruct X0.
+ apply nd_rule.
+ apply x.
+ Defined.
+
+ Instance OrgPCF Γ Δ lev : @ND_Relation _ (PCFRule Γ Δ lev) :=
+ { ndr_eqv := fun a b f g => (pcfToND _ _ _ _ _ f) === (pcfToND _ _ _ _ _ g) }.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ Defined.
+
+ (*
+ * An intermediate representation necessitated by Coq's termination
+ * conditions. This is basically a tree where each node is a
+ * subproof which is either entirely level-1 or entirely level-0
+ *)
+ Inductive Alternating : Tree ??Judg -> Type :=