1 (*********************************************************************************************************************************)
2 (* HaskProofFlattener: *)
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 HaskLiteralsAndTyCons.
18 Require Import HaskStrongTypes.
19 Require Import HaskProof.
20 Require Import NaturalDeduction.
21 Require Import NaturalDeductionCategory.
23 Require Import Algebras_ch4.
24 Require Import Categories_ch1_3.
25 Require Import Functors_ch1_4.
26 Require Import Isomorphisms_ch1_5.
27 Require Import ProductCategories_ch1_6_1.
28 Require Import OppositeCategories_ch1_6_2.
29 Require Import Enrichment_ch2_8.
30 Require Import Subcategories_ch7_1.
31 Require Import NaturalTransformations_ch7_4.
32 Require Import NaturalIsomorphisms_ch7_5.
33 Require Import MonoidalCategories_ch7_8.
34 Require Import Coherence_ch7_8.
36 Require Import HaskStrongTypes.
37 Require Import HaskStrong.
38 Require Import HaskProof.
39 Require Import HaskStrongToProof.
40 Require Import HaskProofToStrong.
41 Require Import ProgrammingLanguage.
42 Require Import HaskProofStratified.
48 * The flattening transformation. Currently only TWO-level languages are
49 * supported, and the level-1 sublanguage is rather limited.
51 * This file abuses terminology pretty badly. For purposes of this file,
52 * "PCF" means "the level-1 sublanguage" and "FC" (aka System FC) means
53 * the whole language (level-0 language including bracketed level-1 terms)
55 Section HaskProofFlattener.
59 Definition code2garrow0 {Γ}(ec t1 t2:RawHaskType Γ ★) : RawHaskType Γ ★.
62 Definition code2garrow Γ (ec t:RawHaskType Γ ★) :=
64 (* | TApp ★ ★ (TApp _ ★ TArrow tx) t' => code2garrow0 ec tx t'*)
65 | _ => code2garrow0 ec unitType t
68 Fixpoint typeMap {TV}{κ}(ty:@RawHaskType TV κ) : @RawHaskType TV κ :=
69 match ty as TY in RawHaskType _ K return RawHaskType TV K with
70 | TCode ec t => code2garrow _ ec t
71 | TApp _ _ t1 t2 => TApp (typeMap t1) (typeMap t2)
72 | TAll _ f => TAll _ (fun tv => typeMap (f tv))
73 | TCoerc _ t1 t2 t3 => TCoerc (typeMap t1) (typeMap t2) (typeMap t3)
77 | TyFunApp tf rhtl => (* FIXME *) TyFunApp tf rhtl
83 Definition code2garrow Γ (ec t:RawHaskType Γ ★) :=
85 (* | TApp ★ ★ (TApp _ ★ TArrow tx) t' => code2garrow0 ec tx t'*)
86 | _ => code2garrow0 ec unitType t
89 Fixpoint typeMap {TV}{κ}(ty:@RawHaskType TV κ) : @RawHaskType TV κ :=
90 match ty as TY in RawHaskType _ K return RawHaskType TV K with
91 | TCode ec t => code2garrow _ ec t
92 | TApp _ _ t1 t2 => TApp (typeMap t1) (typeMap t2)
93 | TAll _ f => TAll _ (fun tv => typeMap (f tv))
94 | TCoerc _ t1 t2 t3 => TCoerc (typeMap t1) (typeMap t2) (typeMap t3)
98 | TyFunApp tf rhtl => (* FIXME *) TyFunApp tf rhtl
101 Definition typeMapL {Γ}(lht:LeveledHaskType Γ ★) : LeveledHaskType Γ ★ :=
103 (* | t @@ nil => (fun TV ite => typeMap (t TV ite)) @@ lev*)
104 | t @@ lev => (fun TV ite => typeMap (t TV ite)) @@ lev
108 (* gathers a tree of guest-language types into a single host-language types via the tensor *)
109 Definition tensorizeType {Γ} (lt:Tree ??(HaskType Γ ★)) : HaskType Γ ★.
113 Definition mkGA {Γ} : HaskType Γ ★ -> HaskType Γ ★ -> HaskType Γ ★.
117 Definition guestJudgmentAsGArrowType {Γ}{Δ}{ec}(lt:PCFJudg Γ Δ ec) : HaskType Γ ★ :=
120 (mkGA (tensorizeType x) (tensorizeType y))
123 Definition obact {Γ}{Δ} ec (X:Tree ??(PCFJudg Γ Δ ec)) : Tree ??(LeveledHaskType Γ ★) :=
124 mapOptionTree guestJudgmentAsGArrowType X @@@ nil.
126 Hint Constructors Rule_Flat.
127 Context {ndr:@ND_Relation _ Rule}.
130 * Here it is, what you've all been waiting for! When reading this,
131 * it might help to have the definition for "Inductive ND" (see
132 * NaturalDeduction.v) handy as a cross-reference.
134 Definition FlatteningFunctor_fmor {Γ}{Δ}{ec}
136 (h~~{JudgmentsL _ _ (PCF _ Γ Δ ec)}~~>c) ->
137 ((obact ec h)~~{TypesL _ _ (SystemFCa _ Γ Δ)}~~>(obact ec c)).
139 set (@nil (HaskTyVar Γ ★)) as lev.
141 unfold hom; unfold ob; unfold ehom; simpl; unfold mon_i; unfold obact; intros.
145 (* the proof from no hypotheses of no conclusions (nd_id0) becomes RVoid *)
146 apply nd_rule; apply (org_fc _ _ (RVoid _ _ )). auto.
148 (* the proof from hypothesis X of conclusion X (nd_id1) becomes RVar *)
149 apply nd_rule; apply (org_fc _ _ (RVar _ _ _ _)). auto.
151 (* the proof from hypothesis X of no conclusions (nd_weak) becomes RWeak;;RVoid *)
155 ; eapply (org_fc _ _ (RArrange _ _ _ _ _ (RWeak _)))
158 eapply (org_fc _ _ (RVoid _ _)); auto.
160 (* the proof from hypothesis X of two identical conclusions X,,X (nd_copy) becomes RVar;;RJoin;;RCont *)
161 eapply nd_comp; [ idtac | eapply nd_rule; eapply (org_fc _ _ (RArrange _ _ _ _ _ (RCont _))) ].
162 eapply nd_comp; [ apply nd_llecnac | idtac ].
163 set (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))
164 (mapOptionTree guestJudgmentAsGArrowType h @@@ lev)) as q.
170 eapply (org_fc _ _ (RJoin _ _ _ _ _ _ )).
174 (* nd_prod becomes nd_llecnac;;nd_prod;;RJoin *)
176 apply (nd_llecnac ;; nd_prod IHX1 IHX2).
178 eapply (org_fc _ _ (RJoin _ _ _ _ _ _ )).
181 (* nd_comp becomes pl_subst (aka nd_cut) *)
183 apply (nd_llecnac ;; nd_prod IHX1 IHX2).
184 clear IHX1 IHX2 X1 X2.
185 apply (@nd_cut _ _ _ _ _ _ (@pl_subst _ _ _ _ (SystemFCa _ Γ Δ))).
187 (* nd_cancell becomes RVar;;RuCanL *)
189 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RuCanL _))) ].
190 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
193 (* nd_cancelr becomes RVar;;RuCanR *)
195 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RuCanR _))) ].
196 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
199 (* nd_llecnac becomes RVar;;RCanL *)
201 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RCanL _))) ].
202 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
205 (* nd_rlecnac becomes RVar;;RCanR *)
207 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RCanR _))) ].
208 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
211 (* nd_assoc becomes RVar;;RAssoc *)
213 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RAssoc _ _ _))) ].
214 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
217 (* nd_cossa becomes RVar;;RCossa *)
219 [ idtac | eapply nd_rule; apply (org_fc _ _ (RArrange _ _ _ _ _ (RCossa _ _ _))) ].
220 apply (nd_seq_reflexive(SequentCalculus:=@pl_sc _ _ _ _ (SystemFCa _ Γ Δ))).
223 destruct r as [r rp].
224 refine (match rp as R in @Rule_PCF _ _ _ H C _ with
225 | PCF_RArrange h c r q => let case_RURule := tt in _
226 | PCF_RLit lit => let case_RLit := tt in _
227 | PCF_RNote Σ τ n => let case_RNote := tt in _
228 | PCF_RVar σ => let case_RVar := tt in _
229 | PCF_RLam Σ tx te => let case_RLam := tt in _
230 | PCF_RApp Σ tx te p => let case_RApp := tt in _
231 | PCF_RLet Σ σ₁ σ₂ p => let case_RLet := tt in _
232 | PCF_RJoin b c d e => let case_RJoin := tt in _
233 | PCF_RVoid => let case_RVoid := tt in _
234 (*| PCF_RCase T κlen κ θ l x => let case_RCase := tt in _*)
235 (*| PCF_RLetRec Σ₁ τ₁ τ₂ lev => let case_RLetRec := tt in _*)
239 rename r0 into r; rename h0 into h; rename c0 into c.
241 destruct case_RURule.
243 | RLeft a b c r => let case_RLeft := tt in _
244 | RRight a b c r => let case_RRight := tt in _
245 | RCanL b => let case_RCanL := tt in _
246 | RCanR b => let case_RCanR := tt in _
247 | RuCanL b => let case_RuCanL := tt in _
248 | RuCanR b => let case_RuCanR := tt in _
249 | RAssoc b c d => let case_RAssoc := tt in _
250 | RCossa b c d => let case_RCossa := tt in _
251 | RExch b c => let case_RExch := tt in _
252 | RWeak b => let case_RWeak := tt in _
253 | RCont b => let case_RCont := tt in _
254 | RComp a b c f g => let case_RComp := tt in _
265 destruct case_RuCanL.
269 destruct case_RuCanR.
273 destruct case_RAssoc.
277 destruct case_RCossa.
297 destruct case_RRight.
309 (* hey cool, I figured out how to pass CoreNote's through... *)
313 eapply (org_fc _ _ (RVar _ _ _ _)) . auto.
315 apply (org_fc _ _ (RNote _ _ _ _ _ n)). auto.
322 (* ga_curry, but try to avoid this someday in the future if the argument type isn't a function *)
330 (* ga_comp! perhaps this means the ga_curry avoidance can be done by turning lambdas into lets? *)
338 (* ga_first+ga_second; technically this assumes a specific evaluation order, which is bad *)
343 Instance FlatteningFunctor {Γ}{Δ}{ec} : Functor (JudgmentsL _ _ (PCF _ Γ Δ ec)) (TypesL _ _ (SystemFCa _ Γ Δ)) (obact ec) :=
344 { fmor := FlatteningFunctor_fmor }.
350 Definition ReificationFunctor Γ Δ : Functor (JudgmentsL _ _ (PCF n Γ Δ)) SystemFCa' (mapOptionTree brakifyJudg).
351 refine {| fmor := ReificationFunctor_fmor Γ Δ |}; unfold hom; unfold ob; simpl ; intros.
352 unfold ReificationFunctor_fmor; simpl.
354 unfold ReificationFunctor_fmor; simpl.
356 unfold ReificationFunctor_fmor; simpl.
361 Definition PCF_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
362 refine {| plsmme_pl := PCF n Γ Δ |}.
366 Definition SystemFCa_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
367 refine {| plsmme_pl := SystemFCa n Γ Δ |}.
371 Definition ReificationFunctorMonoidal n : MonoidalFunctor (JudgmentsN n) (JudgmentsN (S n)) (ReificationFunctor n).
376 Definition PCF_SystemFCa_two_level n Γ Δ : TwoLevelLanguage (PCF_SMME n Γ Δ) (SystemFCa_SMME (S n) Γ Δ).
378 (* ... and the retraction exists *)
381 (* Any particular proof in HaskProof is only finitely large, so it uses only finitely many levels of nesting, so
382 * it falls within (SystemFCa n) for some n. This function calculates that "n" and performs the translation *)
384 Definition HaskProof_to_SystemFCa :
385 forall h c (pf:ND Rule h c),
386 { n:nat & h ~~{JudgmentsL (SystemFCa_SMME n)}~~> c }.
389 (* for every n we have a functor from the category of (n+1)-bounded proofs to the category of n-bounded proofs *)
391 End HaskProofFlattener.