+ >> return () -- TOMDO: what about the coercion?
+ -- we should consider family instances
+
+-- Type & Term reconstruction
+cvObtainTerm :: HscEnv -> Int -> Bool -> Maybe Type -> HValue -> IO Term
+cvObtainTerm hsc_env bound force mb_ty hval = runTR hsc_env $ do
+ tv <- newVar argTypeKind
+ case mb_ty of
+ Nothing -> go bound tv tv hval
+ >>= zonkTerm
+ >>= return . expandNewtypes
+ Just ty | isMonomorphic ty -> go bound ty ty hval
+ >>= zonkTerm
+ >>= return . expandNewtypes
+ Just ty -> do
+ (ty',rev_subst) <- instScheme (sigmaType ty)
+ addConstraint tv ty'
+ term <- go bound tv tv hval >>= zonkTerm
+ --restore original Tyvars
+ return$ expandNewtypes $ mapTermType (substTy rev_subst) term
+ where
+ go bound _ _ _ | seq bound False = undefined
+ go 0 tv _ty a = do
+ clos <- trIO $ getClosureData a
+ return (Suspension (tipe clos) (Just tv) a Nothing)
+ go bound tv ty a = do
+ let monomorphic = not(isTyVarTy tv)
+ -- This ^^^ is a convention. The ancestor tests for
+ -- monomorphism and passes a type instead of a tv
+ clos <- trIO $ getClosureData a
+ case tipe clos of
+-- Thunks we may want to force
+-- NB. this won't attempt to force a BLACKHOLE. Even with :force, we never
+-- force blackholes, because it would almost certainly result in deadlock,
+-- and showing the '_' is more useful.
+ t | isThunk t && force -> seq a $ go (pred bound) tv ty a
+-- We always follow indirections
+ Indirection _ -> go bound tv ty $! (ptrs clos ! 0)
+ -- The interesting case
+ Constr -> do
+ Right dcname <- dataConInfoPtrToName (infoPtr clos)
+ (_,mb_dc) <- tryTcErrs (tcLookupDataCon dcname)
+ case mb_dc of
+ Nothing -> do -- This can happen for private constructors compiled -O0
+ -- where the .hi descriptor does not export them
+ -- In such case, we return a best approximation:
+ -- ignore the unpointed args, and recover the pointeds
+ -- This preserves laziness, and should be safe.
+ let tag = showSDoc (ppr dcname)
+ vars <- replicateM (length$ elems$ ptrs clos)
+ (newVar (liftedTypeKind))
+ subTerms <- sequence [appArr (go (pred bound) tv tv) (ptrs clos) i
+ | (i, tv) <- zip [0..] vars]
+ return (Term tv (Left ('<' : tag ++ ">")) a subTerms)
+ Just dc -> do
+ let extra_args = length(dataConRepArgTys dc) -
+ length(dataConOrigArgTys dc)
+ subTtypes = matchSubTypes dc ty
+ (subTtypesP, subTtypesNP) = partition isPointed subTtypes
+ subTermTvs <- sequence
+ [ if isMonomorphic t then return t
+ else (newVar k)
+ | (t,k) <- zip subTtypesP (map typeKind subTtypesP)]
+ -- It is vital for newtype reconstruction that the unification step
+ -- is done right here, _before_ the subterms are RTTI reconstructed
+ when (not monomorphic) $ do
+ let myType = mkFunTys (reOrderTerms subTermTvs
+ subTtypesNP
+ subTtypes)
+ tv
+ (signatureType,_) <- instScheme(dataConRepType dc)
+ addConstraint myType signatureType
+ subTermsP <- sequence $ drop extra_args
+ -- ^^^ all extra arguments are pointed
+ [ appArr (go (pred bound) tv t) (ptrs clos) i
+ | (i,tv,t) <- zip3 [0..] subTermTvs subTtypesP]
+ let unboxeds = extractUnboxed subTtypesNP clos
+ subTermsNP = map (uncurry Prim) (zip subTtypesNP unboxeds)
+ subTerms = reOrderTerms subTermsP subTermsNP
+ (drop extra_args subTtypes)
+ return (Term tv (Right dc) a subTerms)
+-- The otherwise case: can be a Thunk,AP,PAP,etc.
+ tipe_clos ->
+ return (Suspension tipe_clos (Just tv) a Nothing)
+
+ matchSubTypes dc ty
+ | Just (_,ty_args) <- splitTyConApp_maybe (repType ty)
+-- assumption: ^^^ looks through newtypes
+ , isVanillaDataCon dc --TODO non-vanilla case
+ = dataConInstArgTys dc ty_args
+ | otherwise = dataConRepArgTys dc
+
+-- This is used to put together pointed and nonpointed subterms in the
+-- correct order.
+ reOrderTerms _ _ [] = []
+ reOrderTerms pointed unpointed (ty:tys)
+ | isPointed ty = ASSERT2(not(null pointed)
+ , ptext SLIT("reOrderTerms") $$
+ (ppr pointed $$ ppr unpointed))
+ let (t:tt) = pointed in t : reOrderTerms tt unpointed tys
+ | otherwise = ASSERT2(not(null unpointed)
+ , ptext SLIT("reOrderTerms") $$
+ (ppr pointed $$ ppr unpointed))
+ let (t:tt) = unpointed in t : reOrderTerms pointed tt tys
+
+ expandNewtypes t@Term{ ty=ty, subTerms=tt }
+ | Just (tc, args) <- splitNewTyConApp_maybe ty
+ , isNewTyCon tc
+ , wrapped_type <- newTyConInstRhs tc args
+ , Just dc <- maybeTyConSingleCon tc
+ , t' <- expandNewtypes t{ ty = wrapped_type
+ , subTerms = map expandNewtypes tt }
+ = NewtypeWrap ty (Right dc) t'
+
+ | otherwise = t{ subTerms = map expandNewtypes tt }
+
+ expandNewtypes t = t