+ 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_RVoid : Rule_PCF ec [ ] [ pcfjudg [] [] ] (RVoid Γ Δ )
+(*| PCF_RLetRec : ∀ Σ₁ τ₁ τ₂ , Rule_PCF (ec::nil) _ _ (RLetRec Γ Δ Σ₁ τ₁ τ₂ (ec::nil) )*)
+ | PCF_RJoin : ∀ Σ₁ Σ₂ τ₁ τ₂, Rule_PCF ec ([pcfjudg _ _],,[pcfjudg _ _]) [pcfjudg (_,,_) (_,,_)]
+ (RJoin Γ Δ (Σ₁@@@(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.