1 (*********************************************************************************************************************************)
4 (* Skolemizes the portion of a proof which uses judgments at level >0 *)
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 HaskCoreVars.
18 Require Import HaskWeakTypes.
19 Require Import HaskWeakVars.
20 Require Import HaskLiterals.
21 Require Import HaskTyCons.
22 Require Import HaskStrongTypes.
23 Require Import HaskProof.
24 Require Import NaturalDeduction.
26 Require Import HaskStrongTypes.
27 Require Import HaskStrong.
28 Require Import HaskProof.
29 Require Import HaskStrongToProof.
30 Require Import HaskProofToStrong.
31 Require Import HaskWeakToStrong.
34 Set Printing Width 130.
36 Section HaskSkolemizer.
39 Fixpoint debruijn2phoas {κ} (exp: RawHaskType (fun _ => nat) κ) : HaskType TV κ :=
42 | TAll _ y => TAll _ (fun v => debruijn2phoas (y (TVar v)))
43 | TApp _ _ x y => TApp (debruijn2phoas x) (debruijn2phoas y)
45 | TCoerc _ t1 t2 t => TCoerc (debruijn2phoas t1) (debruijn2phoas t2) (debruijn2phoas t)
47 | TCode v e => TCode (debruijn2phoas v) (debruijn2phoas e)
48 | TyFunApp tfc kl k lt => TyFunApp tfc kl k (debruijn2phoasyFunApp _ lt)
50 with debruijn2phoasyFunApp (lk:list Kind)(exp:@RawHaskTypeList (fun _ => nat) lk) : @HaskTypeList TV lk :=
51 match exp in @RawHaskTypeList _ LK return @RawHaskTypeList TV LK with
52 | TyFunApp_nil => TyFunApp_nil
53 | TyFunApp_cons κ kl t rest => TyFunApp_cons _ _ (debruijn2phoas t) (debruijn2phoasyFunApp _ rest)
56 Definition isNotBrakOrEsc {h}{c} (r:Rule h c) : Prop :=
58 | RBrak _ _ _ _ _ _ => False
59 | REsc _ _ _ _ _ _ => False
63 Fixpoint mkArrows {Γ}(lt:list (HaskType Γ ★))(t:HaskType Γ ★) : HaskType Γ ★ :=
66 | a::b => mkArrows b (a ---> t)
70 Fixpoint unleaves_ {Γ}(t:Tree ??(LeveledHaskType Γ ★))(l:list (HaskType Γ ★)) lev : Tree ??(LeveledHaskType Γ ★) :=
73 | a::b => unleaves_ (t,,[a @@ lev]) b lev
76 (* weak inverse of "leaves" *)
77 Fixpoint unleaves_ {A:Type}(l:list A) : Tree (option A) :=
81 | (a::b) => [a],,(unleaves_ b)
84 (* rules of skolemized proofs *)
85 Definition getΓ (j:Judg) := match j with Γ > _ > _ |- _ @ _ => Γ end.
87 Fixpoint take_trustme {Γ}
89 (l:forall TV, InstantiatedTypeEnv TV Γ -> list (RawHaskType TV ★))
90 : list (HaskType Γ ★) :=
94 | S n' => (fun TV ite => match l TV ite with
95 | nil => Prelude_error "impossible"
99 take_trustme n' (fun TV ite => match l TV ite with
100 | nil => Prelude_error "impossible"
105 Axiom phoas_extensionality : forall Γ Q (f g:forall TV, InstantiatedTypeEnv TV Γ -> Q TV),
106 (forall tv ite, f tv ite = g tv ite) -> f=g.
108 Definition take_arg_types_as_tree {Γ}(ht:HaskType Γ ★) : Tree ??(HaskType Γ ★ ) :=
111 (count_arg_types (ht _ (ite_unit _)))
112 (fun TV ite => take_arg_types (ht TV ite))).
114 Definition drop_arg_types_as_tree {Γ} (ht:HaskType Γ ★) : HaskType Γ ★ :=
115 fun TV ite => drop_arg_types (ht TV ite).
117 Implicit Arguments take_arg_types_as_tree [[Γ]].
118 Implicit Arguments drop_arg_types_as_tree [[Γ]].
120 Definition take_arrange : forall {Γ} (tx te:HaskType Γ ★) lev,
121 Arrange ([tx @@ lev],,take_arg_types_as_tree te @@@ lev)
122 (take_arg_types_as_tree (tx ---> te) @@@ lev).
124 destruct (eqd_dec ([tx],,take_arg_types_as_tree te) (take_arg_types_as_tree (tx ---> te))).
128 unfold take_arg_types_as_tree.
129 Opaque take_arg_types_as_tree.
131 destruct (count_arg_types (te (fun _ : Kind => unit) (ite_unit Γ))).
133 replace (tx) with (fun (TV : Kind → Type) (ite : InstantiatedTypeEnv TV Γ) => tx TV ite).
135 apply phoas_extensionality.
137 apply (Prelude_error "should not be possible").
139 Transparent take_arg_types_as_tree.
141 Definition take_unarrange : forall {Γ} (tx te:HaskType Γ ★) lev,
142 Arrange (take_arg_types_as_tree (tx ---> te) @@@ lev)
143 ([tx @@ lev],,take_arg_types_as_tree te @@@ lev).
145 destruct (eqd_dec ([tx],,take_arg_types_as_tree te) (take_arg_types_as_tree (tx ---> te))).
149 unfold take_arg_types_as_tree.
150 Opaque take_arg_types_as_tree.
152 destruct (count_arg_types (te (fun _ : Kind => unit) (ite_unit Γ))).
154 replace (tx) with (fun (TV : Kind → Type) (ite : InstantiatedTypeEnv TV Γ) => tx TV ite).
156 apply phoas_extensionality.
158 apply (Prelude_error "should not be possible").
160 Transparent take_arg_types_as_tree.
162 Lemma drop_works : forall {Γ}(t1 t2:HaskType Γ ★),
163 drop_arg_types_as_tree (t1 ---> t2) = (drop_arg_types_as_tree t2).
165 unfold drop_arg_types_as_tree.
170 Inductive SRule : Tree ??Judg -> Tree ??Judg -> Type :=
171 (* | SFlat : forall h c (r:Rule h c), isNotBrakOrEsc r -> SRule h c*)
172 | SFlat : forall h c, Rule h c -> SRule h c
173 | SBrak : forall Γ Δ t ec Σ l,
175 [Γ > Δ > Σ,,(take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t ] @ (ec::l)]
176 [Γ > Δ > Σ |- [<[ec |- t]> ] @l]
178 | SEsc : forall Γ Δ t ec Σ l,
180 [Γ > Δ > Σ |- [<[ec |- t]> ] @l]
181 [Γ > Δ > Σ,,(take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t ] @ (ec::l)]
184 Definition take_arg_types_as_tree' {Γ}(lt:LeveledHaskType Γ ★) :=
185 match lt with t @@ l => take_arg_types_as_tree t @@@ l end.
187 Definition drop_arg_types_as_tree' {Γ}(lt:LeveledHaskType Γ ★) :=
188 match lt with t @@ l => drop_arg_types_as_tree t @@ l end.
190 Definition skolemize_judgment (j:Judg) : Judg :=
192 | Γ > Δ > Σ₁ |- Σ₂ @ nil => j
193 | Γ > Δ > Σ₁ |- Σ₂ @ lev =>
194 Γ > Δ > Σ₁,,(mapOptionTreeAndFlatten take_arg_types_as_tree Σ₂ @@@ lev) |- mapOptionTree drop_arg_types_as_tree Σ₂ @ lev
197 Definition check_hof : forall {Γ}(t:HaskType Γ ★),
200 (take_arg_types_as_tree t = [] /\ drop_arg_types_as_tree t = t).
202 destruct (eqd_dec (take_arg_types_as_tree t) []);
203 destruct (eqd_dec (drop_arg_types_as_tree t) t).
210 Opaque take_arg_types_as_tree.
211 Definition skolemize_proof :
214 ND SRule (mapOptionTree skolemize_judgment h) (mapOptionTree skolemize_judgment c).
216 eapply nd_map'; [ idtac | apply X ].
220 refine (match X as R in Rule H C with
221 | RArrange Γ Δ a b x l d => let case_RArrange := tt in _
222 | RNote Γ Δ Σ τ l n => let case_RNote := tt in _
223 | RLit Γ Δ l _ => let case_RLit := tt in _
224 | RVar Γ Δ σ lev => let case_RVar := tt in _
225 | RGlobal Γ Δ σ l wev => let case_RGlobal := tt in _
226 | RLam Γ Δ Σ tx te lev => let case_RLam := tt in _
227 | RCast Γ Δ Σ σ τ lev γ => let case_RCast := tt in _
228 | RAbsT Γ Δ Σ κ σ lev => let case_RAbsT := tt in _
229 | RAppT Γ Δ Σ κ σ τ lev => let case_RAppT := tt in _
230 | RAppCo Γ Δ Σ κ σ₁ σ₂ γ σ lev => let case_RAppCo := tt in _
231 | RAbsCo Γ Δ Σ κ σ σ₁ σ₂ lev => let case_RAbsCo := tt in _
232 | RApp Γ Δ Σ₁ Σ₂ tx te lev => let case_RApp := tt in _
233 | RLet Γ Δ Σ₁ Σ₂ σ₁ σ₂ lev => let case_RLet := tt in _
234 | RWhere Γ Δ Σ₁ Σ₂ Σ₃ σ₁ σ₂ lev => let case_RWhere := tt in _
235 | RJoin Γ p lri m x q l => let case_RJoin := tt in _
236 | RVoid _ _ l => let case_RVoid := tt in _
237 | RBrak Γ Δ t ec succ lev => let case_RBrak := tt in _
238 | REsc Γ Δ t ec succ lev => let case_REsc := tt in _
239 | RCase Γ Δ lev tc Σ avars tbranches alts => let case_RCase := tt in _
240 | RLetRec Γ Δ lri x y t => let case_RLetRec := tt in _
243 destruct case_RArrange.
258 destruct lev; [ idtac | apply (Prelude_error "Brak with nesting depth >1") ].
264 destruct lev; [ idtac | apply (Prelude_error "Esc with nesting depth >1") ].
284 set (check_hof (@literalType l Γ)) as hof.
285 destruct hof; [ apply (Prelude_error "attempt to use a literal with higher-order type at depth>0") | idtac ].
290 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; apply RuCanL ].
298 apply nd_rule; apply SFlat; apply RVar.
299 set (check_hof σ) as hof.
300 destruct hof; [ apply (Prelude_error "attempt to use a variable with higher-order type at depth>0") | idtac ].
305 eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RuCanR ].
310 destruct case_RGlobal.
313 apply nd_rule; apply SFlat; apply RGlobal.
314 set (check_hof (l wev)) as hof.
315 destruct hof; [ apply (Prelude_error "attempt to use a global with higher-order type at depth>0") | idtac ].
320 eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RuCanR ].
348 apply (Prelude_error "found RCast at level >0").
356 apply (Prelude_error "found RJoin at level >0").
365 set (check_hof tx) as hof_tx.
366 destruct hof_tx; [ apply (Prelude_error "attempt tp apply a higher-order function at depth>0") | idtac ].
372 eapply nd_prod; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR ].
377 eapply take_unarrange.
379 eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ].
380 eapply nd_rule; eapply SFlat; apply RWhere.
388 set (check_hof σ₁) as hof_tx.
389 destruct hof_tx; [ apply (Prelude_error "attempt to let-bind a higher-order function at depth>0") | idtac ].
395 eapply nd_prod; [ eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR | eapply nd_id ].
397 set (@RLet Γ Δ Σ₁ (Σ₂,,(take_arg_types_as_tree σ₂ @@@ (h::lev))) σ₁ (drop_arg_types_as_tree σ₂) (h::lev)) as q.
398 eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ].
399 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply q ].
407 destruct case_RWhere.
413 set (check_hof σ₁) as hof_tx.
414 destruct hof_tx; [ apply (Prelude_error "attempt to let-bind a higher-order function at depth>0") | idtac ].
420 eapply nd_prod; [ apply nd_id | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR ].
421 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RAssoc ].
422 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RLeft; eapply RAssoc ].
423 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RWhere ].
424 apply nd_prod; [ idtac | eapply nd_id ].
425 eapply nd_rule; apply SFlat; eapply RArrange.
437 eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RuCanL ].
444 destruct lev; [ apply nd_rule; apply SFlat; apply RAppT | idtac ].
445 apply (Prelude_error "RAppT at depth>0").
449 destruct lev; simpl; [ apply nd_rule; apply SFlat; apply (@RAbsT _ _ _ _ _ nil) | idtac ].
450 apply (Prelude_error "RAbsT at depth>0").
452 destruct case_RAppCo.
454 destruct lev; [ apply nd_rule; apply SFlat; apply RAppCo | idtac ].
456 apply (Prelude_error "RAppCo at depth>0").
458 destruct case_RAbsCo.
460 destruct lev; [ apply nd_rule; apply SFlat; apply RAbsCo | idtac ].
461 apply (Prelude_error "RAbsCo at depth>0").
463 destruct case_RLetRec.
468 apply (@RLetRec Γ Δ lri x y nil).
469 apply (Prelude_error "RLetRec at depth>0").
473 apply (Prelude_error "CASE: BIG FIXME").
476 Transparent take_arg_types_as_tree.