- fix weakExprToStrongExpr (ce:WeakExpr) {struct ce} : forall Γ Δ φ ψ ξ lev,
- Indexed (fun t' => ???(Expr Γ Δ (cure ξ φ) (weakTypeToType φ t' @@ (map φ lev)))) (weakTypeOfWeakExpr ce) :=
- (match ce as CE return (forall Γ Δ φ ψ ξ lev, Indexed _ (weakTypeOfWeakExpr CE))
- with
- | WEVar v => let case_WEVar := tt in checkit (WEVar v) (fun Γ Δ φ ψ ξ lev => _)
- | WELit lit => let case_WELit := tt in checkit (WELit lit) (fun Γ Δ φ ψ ξ lev => _)
- | WEApp e1 e2 => let case_WEApp := tt in checkit (WEApp e1 e2) (fun Γ Δ φ ψ ξ lev =>
- weakExprToStrongExpr e1 Γ Δ φ ψ ξ lev >>>>= fun te1 e1' =>
- ((weakExprToStrongExpr e2 Γ Δ φ ψ ξ lev) >>>>= fun te2 e2' => _))
- | WETyApp e t => let case_WETyApp := tt in
- checkit (WETyApp e t) (fun Γ Δ φ ψ ξ lev => weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun te' e' => _)
- | WECoApp e t => let case_WECoApp := tt in
- checkit (WECoApp e t) (fun Γ Δ φ ψ ξ lev => weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun te' e' => _)
- | WELam ev e => let case_WELam := tt in
- checkit (WELam ev e) (fun Γ Δ φ ψ ξ lev =>
- let ξ' := @upξ ξ (ev::nil) lev in
- weakExprToStrongExpr e Γ Δ φ ψ ξ' lev >>>>= fun te' e' => _)
- | WECoLam cv e => let case_WECoLam := tt in
- checkit (WECoLam cv e) (fun Γ Δ φ ψ ξ lev => (fun e' => _) (weakExprToStrongExpr e))
- | WEBrak ec e tbody => let case_WEBrak := tt in
- checkit (WEBrak ec e tbody) (fun Γ Δ φ ψ ξ lev => weakExprToStrongExpr e Γ Δ φ ψ ξ (ec::lev) >>>>= fun te' e' => _)
- | WEEsc ec e tbody =>
- checkit (WEEsc ec e tbody) (fun Γ Δ φ ψ ξ lev =>
- match lev as LEV return lev=LEV -> _ with
- | nil => let case_WEEsc_bogus := tt in _
- | ec'::lev' => fun ecpf => weakExprToStrongExpr e Γ Δ φ ψ ξ lev' >>>>= fun te' e' => let case_WEEsc := tt in _
- end (refl_equal _))
- | WETyLam tv e => let case_WETyLam := tt in
- checkit (WETyLam tv e) (fun Γ Δ φ ψ ξ lev => (fun e' => _) (weakExprToStrongExpr e))
- | WENote n e => let case_WENote := tt in
- checkit (WENote n e) (fun Γ Δ φ ψ ξ lev => weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun te' e' => _)
- | WECast e co => let case_WECast := tt in
- checkit (WECast e co) (fun Γ Δ φ ψ ξ lev => weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun te' e' => _)
- | WELet v ve e => let case_WELet := tt in
- checkit (WELet v ve e) (fun Γ Δ φ ψ ξ lev =>
- let ξ' := upξ ξ (v::nil) lev in
- ((weakExprToStrongExpr e Γ Δ φ ψ ξ lev)
- >>>>= (fun te' e' => ((weakExprToStrongExpr ve Γ Δ φ ψ ξ' lev) >>>>= (fun vet' ve' => _)))))
-
- | WELetRec rb e =>
- checkit (WELetRec rb e) (fun Γ Δ φ ψ ξ lev =>
-let ξ' := upξ ξ (map (@fst _ _) (leaves (mLetRecTypesVars rb φ))) lev in
- ((fix mLetRecBindingsToELetRecBindings (mlr:Tree ??(WeakExprVar * WeakExpr)) : forall Γ Δ φ ψ ξ lev,
- ???(ELetRecBindings Γ Δ (cure ξ φ) (map φ lev) (mLetRecTypesVars mlr φ)) :=
- match mlr as MLR return forall Γ Δ φ ψ ξ lev,
- ???(ELetRecBindings Γ Δ (cure ξ φ) (map φ lev) (mLetRecTypesVars MLR φ)) with
- | T_Leaf None => fun Γ Δ φ ψ ξ lev => OK (ELR_nil _ _ _ _)
- | T_Leaf (Some (cv,e)) => fun Γ Δ φ ψ ξ lev =>
- let case_mlr_leaf := tt in weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun me => _
- | T_Branch b1 b2 =>
- fun Γ Δ φ ψ ξ lev =>
- mLetRecBindingsToELetRecBindings b1 Γ Δ φ ψ ξ lev >>= fun x1' =>
- mLetRecBindingsToELetRecBindings b2 Γ Δ φ ψ ξ lev >>= fun x2' =>
- OK (ELR_branch _ _ _ _ _ _ x1' x2')
- end) rb Γ Δ φ ψ ξ' lev) >>= fun rb' => (weakExprToStrongExpr e Γ Δ φ ψ ξ' lev)
- >>>>= fun et' e' =>
- let case_MLLetRec := tt in _)
-
- | WECase e tbranches tc avars alts =>
- checkit (WECase e tbranches tc avars alts) (fun Γ Δ φ ψ ξ lev =>
- list2vecOrFail avars _ (fun _ _ => "number of types provided did not match the tycon's number of universal tyvars in Case")
- >>= fun avars0 =>
- let avars' := vec_map (@weakTypeToType Γ φ) avars0 in
- let tbranches' := @weakTypeToType Γ φ tbranches in
- ((fix caseBranches (alts:Tree ??(AltCon*list WeakTypeVar*list WeakCoerVar*list WeakExprVar*WeakExpr))
- :
- ???(Tree ??{ scb : StrongCaseBranchWithVVs WeakExprVar _ tc avars'
- & Expr (sac_Γ scb Γ)
- (sac_Δ scb Γ avars' (weakCK'' Δ))
- (scbwv_ξ scb (cure ξ φ) (map φ lev))
- (weakLT' (tbranches'@@(map φ lev))) }) :=
- match alts with
- | T_Leaf None => OK []
- | T_Branch b1 b2 => caseBranches b1 >>= fun b1' => caseBranches b2 >>= fun b2' => OK (b1',,b2')
- | T_Leaf (Some (alt,tvars,cvars,vvars,e')) =>
- mkStrongAltConPlusJunk' tc alt >>= fun sac =>
- list2vecOrFail vvars (sac_numExprVars (sac:@StrongAltCon tc))
- (fun _ _ => "number of expression variables provided did not match the datacon's number of fields") >>= fun vars =>
- let scb := @Build_StrongCaseBranchWithVVs WeakExprVar _ tc Γ avars' sac vars in
- let rec
- := @weakExprToStrongExpr e'
- (sac_Γ scb Γ)
- (sac_Δ scb Γ avars' (weakCK'' Δ))
- (sacpj_φ sac Γ φ)
- (let case_psi := tt in _)
- ξ
- lev in (let case_ECase_leaf := tt in _)
- end
- ) alts) >>= fun alts' =>
- weakExprToStrongExpr e Γ Δ φ ψ ξ lev >>>>= fun te' e' =>
- let case_ECase := tt in _)
- end))); clear weakExprToStrongExpr.
-
- destruct case_WEVar; intros.
- matchTypes cte (fst (ξ v)) "HaskWeak EVar".
- rewrite matchTypes_pf.
- matchLevs (snd (ξ v)) lev "HaskWeak EVar".
- rewrite <- matchLevs_pf.
- apply OK.
- apply (EVar _ _ (cure ξ φ)).
-
- destruct case_WELit; intros.
- matchTypes (WTyCon (haskLiteralToTyCon lit)) cte "HaskWeak ELit".
- rewrite <- matchTypes_pf.
- apply OK.
- replace (weakTypeToType φ (WTyCon (haskLiteralToTyCon lit))) with (@literalType lit Γ); [ idtac | reflexivity].
- apply ELit.
-
- destruct case_WELet; intros.
- unfold ξ' in ve'.
- matchTypes te' v "HaskWeak ELet".
- rename matchTypes_pf into matchTypes_pf'.
- matchTypes cte vet' "HaskWeak ELet".
- apply OK.
- eapply ELet.
- apply e'.
- rewrite matchTypes_pf'.
- rewrite matchTypes_pf.
- rewrite upξ_lemma in ve'.
- apply ve'.
-
- destruct case_mlr_leaf; intros.
- simpl.
- destruct cv.
- matchTypes me w "HaskWeak LetRec".
- apply OK.
- apply ELR_leaf.
- rewrite <- matchTypes_pf.
- apply X.
-
- destruct case_MLLetRec; intros.
- matchTypes et' cte "HaskWeak LetRec".
- apply OK.
- unfold ξ' in rb'.
- rewrite (letrec_lemma Γ ξ φ rb lev) in rb'.
- apply (@ELetRec WeakExprVar _ Γ Δ (cure ξ φ) (map φ lev) (weakTypeToType φ cte) _ rb').
- rewrite <- (letrec_lemma Γ ξ φ rb lev).
- rewrite <- matchTypes_pf.
- apply e'.
-
- destruct case_WECast; intros.
- apply OK.
- apply (fun pf => @ECast WeakExprVar _ Γ Δ (cure ξ φ) (weakCoercionToHaskCoercion Γ Δ co) _ _ (map φ lev) pf e').
- apply assume_all_coercions_well_formed.
-
- destruct case_WENote; intros.
- matchTypes te' cte "HaskWeak ENote".
- apply OK.
- apply ENote.
- apply n.
- rewrite <- matchTypes_pf.
- apply e'.
-
- destruct case_WEApp; intros.
- matchTypes te1 (WAppTy (WAppTy WFunTyCon te2) cte) "HaskWeak EApp".
- inversion cte_pf.
- destruct (weakTypeOfWeakExpr e1); try apply (Error error_message).
- simpl in H0.
- destruct w; try apply (Error error_message); inversion H0.
- destruct w1; try apply (Error error_message); inversion H0.
- destruct w1_1; try apply (Error error_message); inversion H0.
- clear H0 H1 H2.
- rewrite matchTypes_pf in e1'.
- simpl in e1'.
- rewrite <- H3.
- apply (OK (EApp _ _ _ _ _ _ e1' e2')).
-
- destruct case_WETyApp; intros.
- inversion cte_pf.
- destruct (weakTypeOfWeakExpr e); simpl in *; inversion H0.
- clear H1.
- destruct w; inversion H0.
- simpl in cte_pf.
- clear cte_pf.
- rename w0 into wt.
- rename t into tsubst.
- rename w into wtv.
- set (@ETyApp WeakExprVar _ Γ Δ wtv
- (substRaw (weakTypeToType (upφ wtv φ) wt))
- (weakTypeToType φ tsubst)
- (cure ξ φ)
- (map φ lev)
- (assume_all_types_well_formed _ _ _)
- ) as q.
-
- (* really messy –– but it works! *)
- matchTypes te' (WForAllTy wtv wt) "HaskWeak ETyApp".
- apply OK.
- rewrite substRaw_lemma in q.
- apply q.
- clear q H1 H0.
- rewrite matchTypes_pf in e'.
- simpl in e'.
- unfold HaskTAll.
- unfold substRaw.
- apply e'.
-
- destruct case_WECoApp; intros.
- inversion cte_pf.
- destruct (weakTypeOfWeakExpr e); simpl in *; inversion H0.
- clear H1.
- destruct w; inversion H0.
- subst.
- destruct t as [ct1 ct2 cc].
- set (@ECoApp WeakExprVar _ Γ Δ (weakCoercionToHaskCoercion Γ Δ (weakCoercion ct1 ct2 cc))
- (weakTypeToType φ ct1) (weakTypeToType φ ct2) (weakTypeToType φ te') (cure ξ φ) (map φ lev)) as q.
- matchTypes w3 te' "HaskWeak ECoApp".
- rewrite matchTypes_pf.
- clear matchTypes_pf.
- matchTypes (WCoFunTy ct1 ct2 te') te' "HaskWeak ECoApp".
- apply OK.
- apply q.
- apply assume_all_coercions_well_formed.
- clear q H0 cte_pf.
- rewrite <- matchTypes_pf in e'.
- simpl in e'.
- apply e'.
-
- destruct case_WELam; intros.
- simpl in cte_pf.
- destruct ev as [evv evt].
- destruct (weakTypeOfWeakExpr e); simpl in cte_pf; inversion cte_pf; try apply (Error error_message).
- matchTypes te' w "HaskWeak ELam".
- rewrite <- matchTypes_pf.
- apply OK.
- simpl.
- eapply ELam.
- apply assume_all_types_well_formed.
- unfold ξ' in e'.
- rewrite upξ_lemma in e'.
- apply e'.
-
- destruct case_WETyLam; intros.
- inversion cte_pf.
- destruct tv.
- destruct (weakTypeOfWeakExpr e).
- inversion H0.
- inversion H0.
- set (e' (k::Γ) (weakCE Δ) (upφ (weakTypeVar c k) φ) (fun x => weakCV (ψ x)) ξ lev) as e''.
- inversion e''; try apply (Error error_message).
- inversion X; try apply (Error error_message).
- apply (Error "FIXME: HaskWeakToStrong: type lambda not yet implemented").
-
- destruct case_WECoLam; intros.
- inversion cte_pf.
- destruct cv.
- destruct (weakTypeOfWeakExpr e).
- inversion H0.
- inversion H0.
- set (e' Γ (weakTypeToType φ w ∼∼∼ weakTypeToType φ w0 :: Δ) φ (weakψ ψ) ξ lev) as q.
- inversion q.
- destruct X; try apply (Error error_message).
- set (kindOfType (weakTypeToType φ w)) as qq.
- destruct qq; try apply (Error error_message).
- apply OK.
- apply ECoLam with (κ:=k).
- apply assume_all_types_well_formed.
- apply assume_all_types_well_formed.
- fold (@weakTypeToType Γ).
- apply e0.
-
- destruct case_WEBrak; intros.
- simpl in cte_pf.
- destruct ec as [ecv eck].
- destruct (weakTypeOfWeakExpr e); inversion cte_pf; try apply (Error error_message).
- simpl.
- matchTypes te' w "HaskWeak EBrak".
- apply OK.
- apply EBrak.
- rewrite matchTypes_pf in e'.
- apply e'.
-
- destruct case_WEEsc_bogus; intros.
- apply (Error "attempt to use escape symbol at level zero").
-
- destruct case_WEEsc; intros.
- rewrite ecpf.
- clear ecpf lev.
- matchTypes te' (WCodeTy ec' cte) "HaskWeak EEsc".
- apply OK.
- apply EEsc.
- rewrite matchTypes_pf in e'.
- simpl in e'.
- apply e'.
-
- destruct case_psi.
- apply (sacpj_ψ sac Γ Δ avars' ψ).
-
- destruct case_ECase_leaf.
- inversion rec; try apply (Error error_message).
- destruct X; try apply (Error error_message).
- matchTypes tbranches t "HaskWeak ECase".
- apply OK.
- apply T_Leaf.
- apply Some.
- apply (existT _ {| scbwv_sac := scb ; scbwv_exprvars := vars |}).
- simpl.
- unfold tbranches'.
- rewrite matchTypes_pf.
- rewrite case_lemma1.
- rewrite <- case_lemma2.
- rewrite case_lemma3.
- apply e0.
-
- destruct case_ECase; intros.
- matchTypes cte tbranches "HaskWeak ECase".
- rewrite matchTypes_pf.
- clear matchTypes_pf.
- matchTypes te' (fold_left WAppTy (vec2list avars0) (WTyCon tc)) "HaskWeak ECase".
- apply OK.
- apply (fun e => @ECase WeakExprVar _ Γ Δ (cure ξ φ) (map φ lev) tc _ _ e alts').
- unfold caseType.
- unfold avars'.
- replace (fold_left HaskAppT (vec2list (vec_map (weakTypeToType φ) avars0)) (HaskTCon tc))
- with (weakTypeToType φ (fold_left WAppTy (vec2list avars0) (WTyCon tc))).
- rewrite <- matchTypes_pf.
- apply e'.
- symmetry.
- rewrite <- vec2list_map_list2vec.
- apply case_lemma4.
- apply tc.
- reflexivity.
- Defined.
+ fix weakExprToStrongExpr
+ (Γ:TypeEnv)
+ (Δ:CoercionEnv Γ)
+ (φ:TyVarResolver Γ)
+ (ψ:CoVarResolver Γ Δ)
+ (ξ:WeakExprVar -> LeveledHaskType Γ ★)
+ (τ:HaskType Γ ★)
+ (lev:HaskLevel Γ)
+ (we:WeakExpr) : ???(@Expr _ WeakExprVarEqDecidable Γ Δ ξ (τ @@ lev) ) :=
+ match we with
+
+ | WEVar v => castExpr "WEVar" (τ @@ lev) (EVar Γ Δ ξ v)
+
+ | WELit lit => castExpr "WELit" (τ @@ lev) (ELit Γ Δ ξ lit lev)
+
+ | WELam ev e => weakTypeToType' φ ev ★ >>= fun tv =>
+ weakTypeOfWeakExpr e >>= fun t =>
+ weakTypeToType' φ t ★ >>= fun τ' =>
+ weakExprToStrongExpr Γ Δ φ ψ (update_ξ ξ ((ev,tv@@lev)::nil)) τ' _ e >>= fun e' =>
+ castExpr "WELam" _ (ELam Γ Δ ξ tv _ _ ev e')
+
+ | WEBrak ec e tbody => weakExprToStrongExpr Γ Δ φ ψ ξ τ ((φ (`ec))::lev) e >>= fun e' =>
+ castExpr "WEBrak" _ (EBrak _ _ _ (φ (`ec)) _ lev e')
+
+ | WEEsc ec e tbody => weakTypeToType' φ tbody ★ >>= fun tbody' =>
+ match lev with
+ | nil => Error "ill-leveled escapification"
+ | ec'::lev' => weakExprToStrongExpr Γ Δ φ ψ ξ (<[ φ (`ec) |- tbody' ]>) lev e
+ >>= fun e' =>
+ castExpr "WEEsc" (τ@@lev) (EEsc _ _ _ (φ (`ec)) _ lev e')
+ end
+ | WENote n e => weakExprToStrongExpr Γ Δ φ ψ ξ τ lev e >>= fun e' => OK (ENote _ _ _ _ n e')
+
+ | WELet v ve ebody => weakTypeToType' φ v ★ >>= fun tv =>
+ weakExprToStrongExpr Γ Δ φ ψ ξ tv lev ve >>= fun ve' =>
+ weakExprToStrongExpr Γ Δ φ ψ (update_ξ ξ ((v,tv@@lev)::nil)) τ lev ebody
+ >>= fun ebody' =>
+ OK (ELet _ _ _ tv _ lev v ve' ebody')
+
+ | WEApp e1 e2 => weakTypeOfWeakExpr e2 >>= fun t2 =>
+ weakTypeToType' φ t2 ★ >>= fun t2' =>
+ weakExprToStrongExpr Γ Δ φ ψ ξ t2' lev e2 >>= fun e2' =>
+ weakExprToStrongExpr Γ Δ φ ψ ξ (t2'--->τ) lev e1 >>= fun e1' =>
+ OK (EApp _ _ _ _ _ _ e1' e2')
+
+ | WETyLam tv e => let φ' := upφ tv φ in
+ weakTypeOfWeakExpr e >>= fun te =>
+ weakTypeToType' φ' te ★ >>= fun τ' =>
+ weakExprToStrongExpr _ (weakCE Δ) φ' (weakCV○ψ) (weakLT○ξ) τ' (weakL lev) e
+ >>= fun e' =>
+ castExpr "WETyLam1" _ e' >>= fun e'' =>
+ castExpr "WETyLam2" _ (ETyLam Γ Δ ξ tv (mkTAll' τ') lev e'')
+
+ | WETyApp e t => weakTypeOfWeakExpr e >>= fun te =>
+ match te with
+ | WForAllTy wtv te' =>
+ let φ' := upφ wtv φ in
+ weakTypeToType' φ' te' ★ >>= fun te'' =>
+ weakExprToStrongExpr Γ Δ φ ψ ξ (mkTAll te'') lev e >>= fun e' =>
+ weakTypeToType' φ t wtv >>= fun t' =>
+ castExpr "WETyApp" _ (ETyApp Γ Δ wtv (mkTAll' te'') t' ξ lev e')
+ | _ => Error ("weakTypeToType: WETyApp body with type "+++te)
+ end
+
+ (* I had all of these working before I switched to a Kind-indexed representation for types; it will take a day or two
+ * to get them back working again *)
+ | WECoApp e t => Error "weakExprToStrongExpr: WECoApp not yet implemented"
+ | WECoLam cv e => Error "weakExprToStrongExpr: WECoLam not yet implemented"
+ | WECast e co => Error "weakExprToStrongExpr: WECast not yet implemented"
+ | WELetRec rb e => Error "weakExprToStrongExpr: WELetRec not yet implemented"
+ | WECase e tbranches tc avars alts => Error "weakExprToStrongExpr: WECase not yet implemented"
+ end)).
+ Defined.
+