+Fixpoint varsTypes {Γ}(t:Tree ??(WeakExprVar * WeakExpr))(φ:TyVarResolver Γ) : Tree ??(CoreVar * HaskType Γ ★) :=
+ match t with
+ | T_Leaf None => []
+ | T_Leaf (Some (wev,e)) => match weakTypeToTypeOfKind φ wev ★ with
+ | OK t' => [((wev:CoreVar),t')]
+ | _ => []
+ end
+ | T_Branch b1 b2 => (varsTypes b1 φ),,(varsTypes b2 φ)
+ end.
+
+Fixpoint mkAvars {Γ}(wtl:list WeakType)(lk:list Kind)(φ:TyVarResolver Γ) : ???(IList Kind (HaskType Γ) lk) :=
+match lk as LK return ???(IList Kind (HaskType Γ) LK) with
+ | nil => match wtl with
+ | nil => OK INil
+ | _ => Error "length mismatch in mkAvars"
+ end
+ | k::lk' => match wtl with
+ | nil => Error "length mismatch in mkAvars"
+ | wt::wtl' =>
+ weakTypeToTypeOfKind φ wt k >>= fun t =>
+ mkAvars wtl' lk' φ >>= fun rest =>
+ OK (ICons _ _ t rest)
+ end
+end.
+
+Fixpoint update_ig (ig:CoreVar -> bool) (vars:list CoreVar) : CoreVar -> bool :=
+ match vars with
+ | nil => ig
+ | v::vars' =>
+ fun v' =>
+ if eqd_dec v v'
+ then false
+ else update_ig ig vars' v'
+ end.
+
+(* does the specified variable occur free in the expression? *)
+Fixpoint doesWeakVarOccur (wev:WeakExprVar)(me:WeakExpr) : bool :=
+ match me with
+ | WELit _ => false
+ | WEVar cv => if eqd_dec (wev:CoreVar) (cv:CoreVar) then true else false
+ | WECast e co => doesWeakVarOccur wev e
+ | WENote n e => doesWeakVarOccur wev e
+ | WETyApp e t => doesWeakVarOccur wev e
+ | WECoApp e co => doesWeakVarOccur wev e
+ | WEBrak _ ec e _ => doesWeakVarOccur wev e
+ | WEEsc _ ec e _ => doesWeakVarOccur wev e
+ | WECSP _ ec e _ => doesWeakVarOccur wev e
+ | WELet cv e1 e2 => doesWeakVarOccur wev e1 || (if eqd_dec (wev:CoreVar) (cv:CoreVar)then false else doesWeakVarOccur wev e2)
+ | WEApp e1 e2 => doesWeakVarOccur wev e1 || doesWeakVarOccur wev e2
+ | WELam cv e => if eqd_dec (wev:CoreVar) (cv:CoreVar) then false else doesWeakVarOccur wev e
+(*
+ | WEKappaApp e1 e2 => doesWeakVarOccur wev e1 || doesWeakVarOccur wev e2
+ | WEKappa cv e => if eqd_dec (wev:CoreVar) (cv:CoreVar) then false else doesWeakVarOccur wev e
+*)
+ | WETyLam cv e => doesWeakVarOccur wev e
+ | WECoLam cv e => doesWeakVarOccur wev e
+ | WECase vscrut escrut tbranches tc avars alts =>
+ doesWeakVarOccur wev escrut ||
+ if eqd_dec (wev:CoreVar) (vscrut:CoreVar) then false else
+ ((fix doesWeakVarOccurAlts alts {struct alts} : bool :=
+ match alts with
+ | T_Leaf None => false
+ | T_Leaf (Some (WeakDEFAULT,_,_,_,e)) => doesWeakVarOccur wev e
+ | T_Leaf (Some (WeakLitAlt lit,_,_,_,e)) => doesWeakVarOccur wev e
+ | T_Leaf (Some ((WeakDataAlt dc), tvars, cvars, evars,e)) => doesWeakVarOccur wev e (* FIXME!!! *)
+ | T_Branch b1 b2 => doesWeakVarOccurAlts b1 || doesWeakVarOccurAlts b2
+ end) alts)
+ | WELetRec mlr e =>
+ doesWeakVarOccur wev e ||
+ (fix doesWeakVarOccurLetRec (mlr:Tree ??(WeakExprVar * WeakExpr)) : bool :=
+ match mlr with
+ | T_Leaf None => false
+ | T_Leaf (Some (cv,e)) => if eqd_dec (wev:CoreVar) (cv:CoreVar) then false else doesWeakVarOccur wev e
+ | T_Branch b1 b2 => doesWeakVarOccurLetRec b1 || doesWeakVarOccurLetRec b2
+ end) mlr
+ end.
+Fixpoint doesWeakVarOccurAlts (wev:WeakExprVar)
+ (alts:Tree ??(WeakAltCon * list WeakTypeVar * list WeakCoerVar * list WeakExprVar * WeakExpr)) : bool :=
+ match alts with
+ | T_Leaf None => false
+ | T_Leaf (Some (WeakDEFAULT,_,_,_,e)) => doesWeakVarOccur wev e
+ | T_Leaf (Some (WeakLitAlt lit,_,_,_,e)) => doesWeakVarOccur wev e
+ | T_Leaf (Some ((WeakDataAlt dc), tvars, cvars, evars,e)) => doesWeakVarOccur wev e (* FIXME!!! *)
+ | T_Branch b1 b2 => doesWeakVarOccurAlts wev b1 || doesWeakVarOccurAlts wev b2
+ end.
+
+Definition checkDistinct :
+ forall {V}(EQ:EqDecidable V)(lv:list V), ???(distinct lv).
+ intros.
+ set (distinct_decidable lv) as q.
+ destruct q.
+ exact (OK d).
+ exact (Error "checkDistinct failed").
+ Defined.
+
+(* FIXME: check the kind of the type of the weakexprvar to support >0 *)
+Definition mkGlobal Γ (τ:HaskType Γ ★) (wev:WeakExprVar) : Global Γ.
+ refine {| glob_kinds := nil |}.
+ apply wev.
+ intros.
+ apply τ.
+ Defined.
+