- (* Rules allowed in PCF; i.e. rules we know how to turn into GArrows *)
- Section RulePCF.
-
- Context {Γ:TypeEnv}{Δ:CoercionEnv Γ}.
-
- (* 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 : forall {h}{c}, Rule h c -> Prop :=
- | PCF_RURule : ∀ h c r , Rule_PCF (RURule Γ Δ h c r)
- | PCF_RLit : ∀ Γ Δ Σ τ , Rule_PCF (RLit Γ Δ Σ τ )
- | PCF_RNote : ∀ Σ τ l n , Rule_PCF (RNote Γ Δ Σ τ l n)
- | PCF_RVar : ∀ σ l, Rule_PCF (RVar Γ Δ σ l)
- | PCF_RLam : ∀ Σ tx te q , Rule_PCF (RLam Γ Δ Σ tx te q )
- | PCF_RApp : ∀ Σ tx te p l, Rule_PCF (RApp Γ Δ Σ tx te p l)
- | PCF_RLet : ∀ Σ σ₁ σ₂ p l, Rule_PCF (RLet Γ Δ Σ σ₁ σ₂ p l)
- | PCF_RBindingGroup : ∀ q a b c d e , Rule_PCF (RBindingGroup q a b c d e)
- | PCF_RCase : ∀ T κlen κ θ l x , Rule_PCF (RCase Γ Δ T κlen κ θ l x) (* FIXME: only for boolean and int *)
- | Flat_REmptyGroup : ∀ q a , Rule_PCF (REmptyGroup q a)
- | Flat_RLetRec : ∀ Γ Δ Σ₁ τ₁ τ₂ lev , Rule_PCF (RLetRec Γ Δ Σ₁ τ₁ τ₂ lev).
-
- (* "RulePCF n" is the type of rules permitted in PCF with n-level deep nesting (so, "RulePCF 0" is flat PCF) *)
- Inductive RulePCF : nat -> Tree ??Judg -> Tree ??Judg -> Type :=
-
- (* any proof using only PCF rules is an n-bounded proof for any n>0 *)
- | pcf_flat : forall n h c (r:Rule h c), Rule_PCF r -> RulePCF n h c
-
- (* any n-bounded proof may be used as an (n+1)-bounded proof by prepending Esc and appending Brak *)
- | pfc_nest : forall n h c, ND (RulePCF n) h c -> RulePCF (S n) (mapOptionTree brakify h) (mapOptionTree brakify c)
- .
- End RulePCF.
-
- Section RuleSystemFC.
-
- Context {Γ:TypeEnv}{Δ:CoercionEnv Γ}.
-
- (* "RuleSystemFCa n" is the type of rules permitted in SystemFC^\alpha with n-level-deep nesting
- * in a fixed Γ,Δ context. This is a subcategory of the "complete" SystemFCa, but it's enough to
- * do the flattening transformation *)
- Inductive RuleSystemFCa : nat -> Tree ??Judg -> Tree ??Judg -> Type :=
- | sfc_flat : forall n h c (r:Rule h c), Rule_Flat r -> RuleSystemFCa n h c
- | sfc_nest : forall n h c, ND (@RulePCF Γ Δ n) h c -> RuleSystemFCa (S n) h c
- .
-
- Context (ndr_systemfca:forall n, @ND_Relation _ (RuleSystemFCa n)).
-
- Hint Constructors Rule_Flat.
-
- Definition SystemFC_SC n : @SequentCalculus _ (RuleSystemFCa n) _ (mkJudg Γ Δ).
- apply Build_SequentCalculus.
- intro a.
- induction a.
- destruct a.
- apply nd_rule.
- destruct l.
- apply sfc_flat with (r:=RVar _ _ _ _).
- auto.
- apply nd_rule.
- apply sfc_flat with (r:=REmptyGroup _ _).
- auto.
- eapply nd_comp; [ apply nd_llecnac | idtac ].
- eapply nd_comp; [ eapply nd_prod | idtac ].
- apply IHa1.
- apply IHa2.
- apply nd_rule.
- apply sfc_flat with (r:=RBindingGroup _ _ _ _ _ _ ).
- auto.
- Defined.
+ 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.