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 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 BinoidalCategories.
34 Require Import PreMonoidalCategories.
35 Require Import MonoidalCategories_ch7_8.
36 Require Import Coherence_ch7_8.
38 Require Import HaskStrongTypes.
39 Require Import HaskStrong.
40 Require Import HaskProof.
41 Require Import HaskStrongToProof.
42 Require Import HaskProofToStrong.
43 Require Import ProgrammingLanguage.
44 Require Import HaskProgrammingLanguage.
50 * The flattening transformation. Currently only TWO-level languages are
51 * supported, and the level-1 sublanguage is rather limited.
53 * This file abuses terminology pretty badly. For purposes of this file,
54 * "PCF" means "the level-1 sublanguage" and "FC" (aka System FC) means
55 * the whole language (level-0 language including bracketed level-1 terms)
57 Section HaskFlattener.
60 Context {Δ:CoercionEnv Γ}.
61 Context {ec:HaskTyVar Γ ★}.
63 Lemma unlev_lemma : forall (x:Tree ??(HaskType Γ ★)) lev, x = mapOptionTree unlev (x @@@ lev).
65 rewrite <- mapOptionTree_compose.
68 destruct a; simpl; auto.
75 Context (ga_rep : Tree ??(HaskType Γ ★) -> HaskType Γ ★ ).
76 Context (ga_type : HaskType Γ ★ -> HaskType Γ ★ -> HaskType Γ ★).
78 (*Notation "a ~~~~> b" := (ND Rule [] [ Γ > Δ > a |- b ]) (at level 20).*)
79 Notation "a ~~~~> b" := (ND (OrgR Γ Δ) [] [ (a , b) ]) (at level 20).
81 Lemma magic : forall a b c,
82 ([] ~~~~> [ga_type a b @@ nil]) ->
83 ([ga_type b c @@ nil] ~~~~> [ga_type a c @@ nil]).
87 Context (ga_lit : ∀ lit, [] ~~~~> [ga_type (ga_rep [] ) (ga_rep [literalType lit])@@ nil]).
88 Context (ga_id : ∀ σ, [] ~~~~> [ga_type (ga_rep σ ) (ga_rep σ )@@ nil]).
89 Context (ga_cancell : ∀ c , [] ~~~~> [ga_type (ga_rep ([],,c)) (ga_rep c )@@ nil]).
90 Context (ga_cancelr : ∀ c , [] ~~~~> [ga_type (ga_rep (c,,[])) (ga_rep c )@@ nil]).
91 Context (ga_uncancell: ∀ c , [] ~~~~> [ga_type (ga_rep c ) (ga_rep ([],,c) )@@ nil]).
92 Context (ga_uncancelr: ∀ c , [] ~~~~> [ga_type (ga_rep c ) (ga_rep (c,,[]) )@@ nil]).
93 Context (ga_assoc : ∀ a b c,[] ~~~~> [ga_type (ga_rep ((a,,b),,c)) (ga_rep (a,,(b,,c)) )@@ nil]).
94 Context (ga_unassoc : ∀ a b c,[] ~~~~> [ga_type (ga_rep ( a,,(b,,c))) (ga_rep ((a,,b),,c)) @@ nil]).
95 Context (ga_swap : ∀ a b, [] ~~~~> [ga_type (ga_rep (a,,b) ) (ga_rep (b,,a) )@@ nil]).
96 Context (ga_copy : ∀ a , [] ~~~~> [ga_type (ga_rep a ) (ga_rep (a,,a) )@@ nil]).
97 Context (ga_drop : ∀ a , [] ~~~~> [ga_type (ga_rep a ) (ga_rep [] )@@ nil]).
98 Context (ga_first : ∀ a b c,
99 [ga_type (ga_rep a) (ga_rep b) @@nil] ~~~~> [ga_type (ga_rep (a,,c)) (ga_rep (b,,c)) @@nil]).
100 Context (ga_second : ∀ a b c,
101 [ga_type (ga_rep a) (ga_rep b) @@nil] ~~~~> [ga_type (ga_rep (c,,a)) (ga_rep (c,,b)) @@nil]).
102 Context (ga_comp : ∀ a b c,
103 ([ga_type (ga_rep a) (ga_rep b) @@nil],,[ga_type (ga_rep b) (ga_rep c) @@nil])
105 [ga_type (ga_rep a) (ga_rep c) @@nil]).
107 Definition guestJudgmentAsGArrowType (lt:PCFJudg Γ ec) : HaskType Γ ★ :=
109 (x,y) => (ga_type (ga_rep x) (ga_rep y))
112 Definition obact (X:Tree ??(PCFJudg Γ ec)) : Tree ??(LeveledHaskType Γ ★) :=
113 mapOptionTree guestJudgmentAsGArrowType X @@@ nil.
115 Hint Constructors Rule_Flat.
116 Context {ndr:@ND_Relation _ Rule}.
119 * Here it is, what you've all been waiting for! When reading this,
120 * it might help to have the definition for "Inductive ND" (see
121 * NaturalDeduction.v) handy as a cross-reference.
123 Hint Constructors Rule_Flat.
124 Definition FlatteningFunctor_fmor
126 (ND (PCFRule Γ Δ ec) h c) ->
127 ((obact h)~~~~>(obact c)).
129 set (@nil (HaskTyVar Γ ★)) as lev.
131 unfold hom; unfold ob; unfold ehom; simpl; unfold pmon_I; unfold obact; intros.
135 (* the proof from no hypotheses of no conclusions (nd_id0) becomes RVoid *)
136 apply nd_rule; apply (org_fc Γ Δ [] [(_,_)] (RVoid _ _)). apply Flat_RVoid.
138 (* the proof from hypothesis X of conclusion X (nd_id1) becomes RVar *)
139 apply nd_rule; apply (org_fc _ _ [] [(_,_)] (RVar _ _ _ _)). apply Flat_RVar.
141 (* the proof from hypothesis X of no conclusions (nd_weak) becomes RWeak;;RVoid *)
145 ; eapply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RWeak _)))
148 eapply (org_fc _ _ [] [(_,_)] (RVoid _ _)); auto. apply Flat_RVoid.
151 (* the proof from hypothesis X of two identical conclusions X,,X (nd_copy) becomes RVar;;RJoin;;RCont *)
152 eapply nd_comp; [ idtac | eapply nd_rule; eapply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RCont _))) ].
153 eapply nd_comp; [ apply nd_llecnac | idtac ].
154 set (snd_initial(SequentND:=pl_snd(ProgrammingLanguage:=SystemFCa Γ Δ))
155 (mapOptionTree (guestJudgmentAsGArrowType) h @@@ lev)) as q.
161 eapply (org_fc _ _ ([(_,_)],,[(_,_)]) [(_,_)] (RJoin _ _ _ _ _ _ )).
170 (* nd_prod becomes nd_llecnac;;nd_prod;;RJoin *)
172 apply (nd_llecnac ;; nd_prod IHX1 IHX2).
174 eapply (org_fc _ _ ([(_,_)],,[(_,_)]) [(_,_)] (RJoin _ _ _ _ _ _ )).
175 apply (Flat_RJoin Γ Δ (mapOptionTree guestJudgmentAsGArrowType h1 @@@ nil)
176 (mapOptionTree guestJudgmentAsGArrowType h2 @@@ nil)
177 (mapOptionTree guestJudgmentAsGArrowType c1 @@@ nil)
178 (mapOptionTree guestJudgmentAsGArrowType c2 @@@ nil)).
180 (* nd_comp becomes pl_subst (aka nd_cut) *)
182 apply (nd_llecnac ;; nd_prod IHX1 IHX2).
183 clear IHX1 IHX2 X1 X2.
184 apply (@snd_cut _ _ _ _ (pl_snd(ProgrammingLanguage:=SystemFCa Γ Δ))).
186 (* nd_cancell becomes RVar;;RuCanL *)
188 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RuCanL _))) ].
189 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
192 (* nd_cancelr becomes RVar;;RuCanR *)
194 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RuCanR _))) ].
195 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
198 (* nd_llecnac becomes RVar;;RCanL *)
200 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RCanL _))) ].
201 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
204 (* nd_rlecnac becomes RVar;;RCanR *)
206 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RCanR _))) ].
207 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
210 (* nd_assoc becomes RVar;;RAssoc *)
212 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RAssoc _ _ _))) ].
213 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
216 (* nd_cossa becomes RVar;;RCossa *)
218 [ idtac | eapply nd_rule; apply (org_fc _ _ [(_,_)] [(_,_)] (RArrange _ _ _ _ _ (RCossa _ _ _))) ].
219 apply (snd_initial(SequentND:=pl_cnd(ProgrammingLanguage:=(SystemFCa Γ Δ)))).
222 destruct r as [r rp].
227 refine (match rp as R in @Rule_PCF _ _ _ H C _
230 [sequent (mapOptionTree guestJudgmentAsGArrowType H @@@ nil)
231 (mapOptionTree guestJudgmentAsGArrowType C @@@ nil)]
233 | PCF_RArrange h c r q => let case_RURule := tt in _
234 | PCF_RLit lit => let case_RLit := tt in _
235 | PCF_RNote Σ τ n => let case_RNote := tt in _
236 | PCF_RVar σ => let case_RVar := tt in _
237 | PCF_RLam Σ tx te => let case_RLam := tt in _
238 | PCF_RApp Σ tx te p => let case_RApp := tt in _
239 | PCF_RLet Σ σ₁ σ₂ p => let case_RLet := tt in _
240 | PCF_RJoin b c d e => let case_RJoin := tt in _
241 | PCF_RVoid => let case_RVoid := tt in _
242 (*| PCF_RCase T κlen κ θ l x => let case_RCase := tt in _*)
243 (*| PCF_RLetRec Σ₁ τ₁ τ₂ lev => let case_RLetRec := tt in _*)
247 rewrite (unlev_lemma h (ec::nil)).
248 rewrite (unlev_lemma c (ec::nil)).
249 destruct case_RURule.
250 refine (match q as Q in Arrange H C
252 H=(h @@@ (ec :: nil)) ->
253 C=(c @@@ (ec :: nil)) ->
256 [ga_type (ga_rep (mapOptionTree unlev H)) (ga_rep r) @@ nil ]
257 [ga_type (ga_rep (mapOptionTree unlev C)) (ga_rep r) @@ nil ] ]
259 | RLeft a b c r => let case_RLeft := tt in _
260 | RRight a b c r => let case_RRight := tt in _
261 | RCanL b => let case_RCanL := tt in _
262 | RCanR b => let case_RCanR := tt in _
263 | RuCanL b => let case_RuCanL := tt in _
264 | RuCanR b => let case_RuCanR := tt in _
265 | RAssoc b c d => let case_RAssoc := tt in _
266 | RCossa b c d => let case_RCossa := tt in _
267 | RExch b c => let case_RExch := tt in _
268 | RWeak b => let case_RWeak := tt in _
269 | RCont b => let case_RCont := tt in _
270 | RComp a b c f g => let case_RComp := tt in _
271 end (refl_equal _) (refl_equal _));
274 try rewrite <- unlev_lemma in *.
284 destruct case_RuCanL.
288 destruct case_RuCanR.
292 destruct case_RAssoc.
296 destruct case_RCossa.
317 destruct case_RRight.
330 (* hey cool, I figured out how to pass CoreNote's through... *)
334 eapply (org_fc _ _ [] [(_,_)] (RVar _ _ _ _)) . auto.
337 apply (org_fc _ _ [(_,_)] [(_,_)] (RNote _ _ _ _ _ n)). auto.
344 * class GArrow g (**) u => GArrowApply g (**) u (~>) where
345 * ga_applyl :: g (x**(x~>y) ) y
346 * ga_applyr :: g ( (x~>y)**x) y
348 * class GArrow g (**) u => GArrowCurry g (**) u (~>) where
349 * ga_curryl :: g (x**y) z -> g x (y~>z)
350 * ga_curryr :: g (x**y) z -> g y (x~>z)
353 (* GArrowCurry.ga_curry *)
357 (* GArrowApply.ga_apply *)
367 (* this assumes we want effects to occur in syntactically-left-to-right order *)
372 Instance FlatteningFunctor {Γ}{Δ}{ec} : Functor (JudgmentsL (PCF Γ Δ ec)) (TypesL (SystemFCa Γ Δ)) (obact) :=
373 { fmor := FlatteningFunctor_fmor }.
376 Definition ReificationFunctor Γ Δ : Functor (JudgmentsL _ _ (PCF n Γ Δ)) SystemFCa' (mapOptionTree brakifyJudg).
377 refine {| fmor := ReificationFunctor_fmor Γ Δ |}; unfold hom; unfold ob; simpl ; intros.
380 Definition PCF_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
381 refine {| plsmme_pl := PCF n Γ Δ |}.
385 Definition SystemFCa_SMME (n:nat)(Γ:TypeEnv)(Δ:CoercionEnv Γ) : ProgrammingLanguageSMME.
386 refine {| plsmme_pl := SystemFCa n Γ Δ |}.
390 Definition ReificationFunctorMonoidal n : MonoidalFunctor (JudgmentsN n) (JudgmentsN (S n)) (ReificationFunctor n).
395 Definition PCF_SystemFCa_two_level n Γ Δ : TwoLevelLanguage (PCF_SMME n Γ Δ) (SystemFCa_SMME (S n) Γ Δ).
399 (* ... and the retraction exists *)