X-Git-Url: http://git.megacz.com/?p=coq-hetmet.git;a=blobdiff_plain;f=src%2FHaskSkolemizer.v;h=bb4dc924c14fb66be24701290e8f3af0cedabc48;hp=c1b570851ac5fb102f0264fbb0256a7fa3d8429b;hb=57e387249da84dac0f1c5a9411e3900831ce2d81;hpb=4a32fb619ddda1fedb0855a0c7acad0a41704da8 diff --git a/src/HaskSkolemizer.v b/src/HaskSkolemizer.v index c1b5708..bb4dc92 100644 --- a/src/HaskSkolemizer.v +++ b/src/HaskSkolemizer.v @@ -66,57 +66,50 @@ Section HaskSkolemizer. | a::b => mkArrows b (a ---> t) end. +(* Fixpoint unleaves_ {Γ}(t:Tree ??(LeveledHaskType Γ ★))(l:list (HaskType Γ ★)) lev : Tree ??(LeveledHaskType Γ ★) := match l with | nil => t | a::b => unleaves_ (t,,[a @@ lev]) b lev end. - - (* rules of skolemized proofs *) - Definition getΓ (j:Judg) := match j with Γ > _ > _ |- _ => Γ end. - Definition getSuc (j:Judg) : Tree ??(LeveledHaskType (getΓ j) ★) := - match j as J return Tree ??(LeveledHaskType (getΓ J) ★) with Γ > _ > _ |- s => s end. - Fixpoint getjlev {Γ}(tt:Tree ??(LeveledHaskType Γ ★)) : HaskLevel Γ := - match tt with - | T_Leaf None => nil - | T_Leaf (Some (_ @@ lev)) => lev - | T_Branch b1 b2 => - match getjlev b1 with - | nil => getjlev b2 - | lev => lev - end +*) + (* weak inverse of "leaves" *) + Fixpoint unleaves_ {A:Type}(l:list A) : Tree (option A) := + match l with + | nil => [] + | (a::nil) => [a] + | (a::b) => [a],,(unleaves_ b) end. - Definition ite_unit : ∀ Γ, InstantiatedTypeEnv (fun _ => unit) Γ. - intros. - induction Γ. - apply INil. - apply ICons; auto. - apply tt. - Defined. + (* rules of skolemized proofs *) + Definition getΓ (j:Judg) := match j with Γ > _ > _ |- _ @ _ => Γ end. - Fixpoint grab (n:nat) {T} (l:list T) : T := - match l with - | nil => Prelude_error "grab failed" - | h::t => match n with - | 0 => h - | S n' => grab n' t - end - end. + Fixpoint take_trustme {Γ} + (n:nat) + (l:forall TV, InstantiatedTypeEnv TV Γ -> list (RawHaskType TV ★)) + : list (HaskType Γ ★) := - Fixpoint count' (n:nat)(ln:list nat) : list nat := match n with - | 0 => ln - | S n' => count' n' (n'::ln) + | 0 => nil + | S n' => (fun TV ite => match l TV ite with + | nil => Prelude_error "impossible" + | a::b => a + end) + :: + take_trustme n' (fun TV ite => match l TV ite with + | nil => Prelude_error "impossible" + | a::b => b + end) end. + + Axiom phoas_extensionality : forall Γ Q (f g:forall TV, InstantiatedTypeEnv TV Γ -> Q TV), + (forall tv ite, f tv ite = g tv ite) -> f=g. - Definition count (n:nat) := count' n nil. - - Definition rebundle {Γ}(X:forall TV, InstantiatedTypeEnv TV Γ -> list (RawHaskType TV ★)) : list (HaskType Γ ★ ) := - map (fun n => fun TV ite => grab n (X TV ite)) (count (length (X _ (ite_unit _)))). - - Definition take_arg_types_as_tree Γ (ht:HaskType Γ ★) := - (unleaves' (rebundle (fun TV ite => (take_arg_types (ht TV ite))))). + Definition take_arg_types_as_tree {Γ}(ht:HaskType Γ ★) : Tree ??(HaskType Γ ★ ) := + unleaves_ + (take_trustme + (count_arg_types (ht _ (ite_unit _))) + (fun TV ite => take_arg_types (ht TV ite))). Definition drop_arg_types_as_tree {Γ} (ht:HaskType Γ ★) : HaskType Γ ★ := fun TV ite => drop_arg_types (ht TV ite). @@ -124,13 +117,47 @@ Section HaskSkolemizer. Implicit Arguments take_arg_types_as_tree [[Γ]]. Implicit Arguments drop_arg_types_as_tree [[Γ]]. - Lemma take_works : forall {Γ}(t1 t2:HaskType Γ ★), - take_arg_types_as_tree (t1 ---> t2) = [t1],,(take_arg_types_as_tree t2). + Definition take_arrange : forall {Γ} (tx te:HaskType Γ ★) lev, + Arrange ([tx @@ lev],,take_arg_types_as_tree te @@@ lev) + (take_arg_types_as_tree (tx ---> te) @@@ lev). intros. - unfold take_arg_types_as_tree; simpl. - unfold rebundle at 1. - admit. - Qed. + destruct (eqd_dec ([tx],,take_arg_types_as_tree te) (take_arg_types_as_tree (tx ---> te))). + rewrite <- e. + simpl. + apply RId. + unfold take_arg_types_as_tree. + Opaque take_arg_types_as_tree. + simpl. + destruct (count_arg_types (te (fun _ : Kind => unit) (ite_unit Γ))). + simpl. + replace (tx) with (fun (TV : Kind → Type) (ite : InstantiatedTypeEnv TV Γ) => tx TV ite). + apply RCanR. + apply phoas_extensionality. + reflexivity. + apply (Prelude_error "should not be possible"). + Defined. + Transparent take_arg_types_as_tree. + + Definition take_unarrange : forall {Γ} (tx te:HaskType Γ ★) lev, + Arrange (take_arg_types_as_tree (tx ---> te) @@@ lev) + ([tx @@ lev],,take_arg_types_as_tree te @@@ lev). + intros. + destruct (eqd_dec ([tx],,take_arg_types_as_tree te) (take_arg_types_as_tree (tx ---> te))). + rewrite <- e. + simpl. + apply RId. + unfold take_arg_types_as_tree. + Opaque take_arg_types_as_tree. + simpl. + destruct (count_arg_types (te (fun _ : Kind => unit) (ite_unit Γ))). + simpl. + replace (tx) with (fun (TV : Kind → Type) (ite : InstantiatedTypeEnv TV Γ) => tx TV ite). + apply RuCanR. + apply phoas_extensionality. + reflexivity. + apply (Prelude_error "should not be possible"). + Defined. + Transparent take_arg_types_as_tree. Lemma drop_works : forall {Γ}(t1 t2:HaskType Γ ★), drop_arg_types_as_tree (t1 ---> t2) = (drop_arg_types_as_tree t2). @@ -145,13 +172,13 @@ Section HaskSkolemizer. | SFlat : forall h c, Rule h c -> SRule h c | SBrak : forall Γ Δ t ec Σ l, SRule - [Γ > Δ > Σ,, (take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t @@ (ec::l) ]] - [Γ > Δ > Σ |- [<[ec |- t]> @@ l ]] + [Γ > Δ > Σ,,(take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t ] @ (ec::l)] + [Γ > Δ > Σ |- [<[ec |- t]> ] @l] | SEsc : forall Γ Δ t ec Σ l, SRule - [Γ > Δ > Σ |- [<[ec |- t]> @@ l ]] - [Γ > Δ > Σ,, (take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t @@ (ec::l) ]] + [Γ > Δ > Σ |- [<[ec |- t]> ] @l] + [Γ > Δ > Σ,,(take_arg_types_as_tree t @@@ (ec::l)) |- [ drop_arg_types_as_tree t ] @ (ec::l)] . Definition take_arg_types_as_tree' {Γ}(lt:LeveledHaskType Γ ★) := @@ -162,11 +189,9 @@ Section HaskSkolemizer. Definition skolemize_judgment (j:Judg) : Judg := match j with - Γ > Δ > Σ₁ |- Σ₂ => - match getjlev Σ₂ with - | nil => j - | lev => Γ > Δ > Σ₁,,(mapOptionTreeAndFlatten take_arg_types_as_tree' Σ₂) |- mapOptionTree drop_arg_types_as_tree' Σ₂ - end + | Γ > Δ > Σ₁ |- Σ₂ @ nil => j + | Γ > Δ > Σ₁ |- Σ₂ @ lev => + Γ > Δ > Σ₁,,(mapOptionTreeAndFlatten take_arg_types_as_tree Σ₂ @@@ lev) |- mapOptionTree drop_arg_types_as_tree Σ₂ @ lev end. Definition check_hof : forall {Γ}(t:HaskType Γ ★), @@ -182,6 +207,7 @@ Section HaskSkolemizer. left; auto. Defined. + Opaque take_arg_types_as_tree. Definition skolemize_proof : forall {h}{c}, ND Rule h c -> @@ -192,7 +218,7 @@ Section HaskSkolemizer. intros. refine (match X as R in Rule H C with - | RArrange Γ Δ a b x d => let case_RArrange := tt in _ + | RArrange Γ Δ a b x l d => let case_RArrange := tt in _ | RNote Γ Δ Σ τ l n => let case_RNote := tt in _ | RLit Γ Δ l _ => let case_RLit := tt in _ | RVar Γ Δ σ lev => let case_RVar := tt in _ @@ -205,17 +231,18 @@ Section HaskSkolemizer. | RAbsCo Γ Δ Σ κ σ σ₁ σ₂ lev => let case_RAbsCo := tt in _ | RApp Γ Δ Σ₁ Σ₂ tx te lev => let case_RApp := tt in _ | RLet Γ Δ Σ₁ Σ₂ σ₁ σ₂ lev => let case_RLet := tt in _ - | RJoin Γ p lri m x q => let case_RJoin := tt in _ - | RVoid _ _ => let case_RVoid := tt in _ - | RBrak Γ Δ t ec succ lev => let case_RBrak := tt in _ - | REsc Γ Δ t ec succ lev => let case_REsc := tt in _ + | RWhere Γ Δ Σ₁ Σ₂ Σ₃ σ₁ σ₂ lev => let case_RWhere := tt in _ + | RJoin Γ p lri m x q l => let case_RJoin := tt in _ + | RVoid _ _ l => let case_RVoid := tt in _ + | RBrak Γ Δ t ec succ lev => let case_RBrak := tt in _ + | REsc Γ Δ t ec succ lev => let case_REsc := tt in _ | RCase Γ Δ lev tc Σ avars tbranches alts => let case_RCase := tt in _ | RLetRec Γ Δ lri x y t => let case_RLetRec := tt in _ end); clear X h c. destruct case_RArrange. simpl. - destruct (getjlev x). + destruct l. apply nd_rule. apply SFlat. apply RArrange. @@ -296,17 +323,20 @@ Section HaskSkolemizer. apply RGlobal. destruct case_RLam. - simpl. destruct lev. - apply nd_rule. - apply SFlat. - apply RLam. - rewrite take_works. + apply nd_rule. + apply SFlat. + simpl. + apply RLam. + simpl. rewrite drop_works. apply nd_rule. - apply SFlat. - apply RArrange. - apply RCossa. + apply SFlat. + apply RArrange. + eapply RComp. + eapply RCossa. + eapply RLeft. + apply take_arrange. destruct case_RCast. simpl. @@ -319,13 +349,11 @@ Section HaskSkolemizer. destruct case_RJoin. simpl. - destruct (getjlev x). - destruct (getjlev q). + destruct l. apply nd_rule. apply SFlat. apply RJoin. apply (Prelude_error "found RJoin at level >0"). - apply (Prelude_error "found RJoin at level >0"). destruct case_RApp. simpl. @@ -333,7 +361,6 @@ Section HaskSkolemizer. apply nd_rule. apply SFlat. apply RApp. - rewrite take_works. rewrite drop_works. set (check_hof tx) as hof_tx. destruct hof_tx; [ apply (Prelude_error "attempt tp apply a higher-order function at depth>0") | idtac ]. @@ -341,18 +368,16 @@ Section HaskSkolemizer. rewrite H. rewrite H0. simpl. - set (@RLet Γ Δ (Σ₂,,take_arg_types_as_tree te @@@ (h :: lev)) Σ₁ (drop_arg_types_as_tree te) tx (h::lev)) as q. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RExch ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply q ]. - clear q. - apply nd_prod. - apply nd_rule. - apply SFlat. - apply RArrange. - apply RCanR. + eapply nd_comp. + eapply nd_prod; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR ]. + eapply nd_rule. + eapply SFlat. + eapply RArrange. + eapply RLeft. + eapply take_unarrange. + eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ]. - apply nd_rule; apply SFlat; apply RArrange; apply RLeft; eapply RExch. + eapply nd_rule; eapply SFlat; apply RWhere. destruct case_RLet. simpl. @@ -360,32 +385,56 @@ Section HaskSkolemizer. apply nd_rule. apply SFlat. apply RLet. - set (check_hof σ₂) as hof_tx. + set (check_hof σ₁) as hof_tx. destruct hof_tx; [ apply (Prelude_error "attempt to let-bind a higher-order function at depth>0") | idtac ]. destruct a. rewrite H. rewrite H0. - set (@RLet Γ Δ (Σ₁,,(take_arg_types_as_tree σ₁ @@@ (h::lev))) Σ₂ (drop_arg_types_as_tree σ₁) σ₂ (h::lev)) as q. + + eapply nd_comp. + eapply nd_prod; [ eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR | eapply nd_id ]. + + set (@RLet Γ Δ Σ₁ (Σ₂,,(take_arg_types_as_tree σ₂ @@@ (h::lev))) σ₁ (drop_arg_types_as_tree σ₂) (h::lev)) as q. eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; eapply RLeft; apply RExch ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RCossa ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply q ]. - clear q. + eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply q ]. apply nd_prod. + apply nd_id. + apply nd_rule. + eapply SFlat. + eapply RArrange. + eapply RCossa. + + destruct case_RWhere. + simpl. + destruct lev. apply nd_rule. apply SFlat. - apply RArrange. - apply RCanR. - eapply nd_comp; [ eapply nd_rule; apply SFlat; eapply RArrange; apply RCossa | idtac ]. - eapply nd_comp; [ idtac | eapply nd_rule; apply SFlat; eapply RArrange; apply RAssoc ]. - apply nd_rule. - apply SFlat. - apply RArrange. + apply RWhere. + set (check_hof σ₁) as hof_tx. + destruct hof_tx; [ apply (Prelude_error "attempt to let-bind a higher-order function at depth>0") | idtac ]. + destruct a. + rewrite H. + rewrite H0. + + eapply nd_comp. + eapply nd_prod; [ apply nd_id | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RCanR ]. + eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RAssoc ]. + eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RLeft; eapply RAssoc ]. + eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RWhere ]. + apply nd_prod; [ idtac | eapply nd_id ]. + eapply nd_rule; apply SFlat; eapply RArrange. + eapply RComp. + eapply RCossa. apply RLeft. - eapply RExch. + eapply RCossa. destruct case_RVoid. simpl. + destruct l. + apply nd_rule. + apply SFlat. + apply RVoid. + eapply nd_comp; [ idtac | eapply nd_rule; eapply SFlat; eapply RArrange; eapply RuCanL ]. apply nd_rule. apply SFlat. apply RVoid. @@ -414,16 +463,16 @@ Section HaskSkolemizer. destruct case_RLetRec. simpl. destruct t. - destruct (getjlev (y @@@ nil)). apply nd_rule. apply SFlat. apply (@RLetRec Γ Δ lri x y nil). apply (Prelude_error "RLetRec at depth>0"). - apply (Prelude_error "RLetRec at depth>0"). destruct case_RCase. simpl. apply (Prelude_error "CASE: BIG FIXME"). Defined. + Transparent take_arg_types_as_tree. + End HaskSkolemizer.