44module Wingman.GHC where
55
66import Bag (bagToList )
7+ import Class (classTyVars )
78import ConLike
8- import Control.Applicative (empty )
99import Control.Monad.State
1010import Control.Monad.Trans.Maybe (MaybeT (.. ))
1111import CoreUtils (exprType )
12+ import Data.Bool (bool )
1213import Data.Function (on )
1314import Data.Functor ((<&>) )
1415import Data.List (isPrefixOf )
@@ -18,22 +19,21 @@ import Data.Set (Set)
1819import qualified Data.Set as S
1920import Data.Traversable
2021import DataCon
21- import Development.IDE (HscEnvEq (hscEnv ))
22- import Development.IDE.Core.Compile (lookupName )
2322import Development.IDE.GHC.Compat hiding (exprType )
2423import DsExpr (dsExpr )
2524import DsMonad (initDs )
2625import FamInst (tcLookupDataFamInst_maybe )
2726import FamInstEnv (normaliseType )
2827import GHC.SourceGen (lambda )
2928import Generics.SYB (Data , everything , everywhere , listify , mkQ , mkT )
30- import GhcPlugins (extractModule , GlobalRdrElt ( gre_name ), Role (Nominal ))
29+ import GhcPlugins (Role (Nominal ))
3130import OccName
3231import TcRnMonad
3332import TcType
3433import TyCoRep
3534import Type
3635import TysWiredIn (charTyCon , doubleTyCon , floatTyCon , intTyCon )
36+ import Unify
3737import Unique
3838import Var
3939import Wingman.Types
@@ -323,40 +323,6 @@ unXPat (XPat (L _ pat)) = unXPat pat
323323unXPat pat = pat
324324
325325
326- ------------------------------------------------------------------------------
327- -- | Build a 'KnownThings'.
328- knownThings :: TcGblEnv -> HscEnvEq -> MaybeT IO KnownThings
329- knownThings tcg hscenv= do
330- let cls = knownClass tcg hscenv
331- KnownThings
332- <$> cls (mkClsOcc " Semigroup" )
333- <*> cls (mkClsOcc " Monoid" )
334-
335-
336- ------------------------------------------------------------------------------
337- -- | Like 'knownThing' but specialized to classes.
338- knownClass :: TcGblEnv -> HscEnvEq -> OccName -> MaybeT IO Class
339- knownClass = knownThing $ \ case
340- ATyCon tc -> tyConClass_maybe tc
341- _ -> Nothing
342-
343-
344- ------------------------------------------------------------------------------
345- -- | Helper function for defining 'knownThings'.
346- knownThing :: (TyThing -> Maybe a ) -> TcGblEnv -> HscEnvEq -> OccName -> MaybeT IO a
347- knownThing f tcg hscenv occ = do
348- let modul = extractModule tcg
349- rdrenv = tcg_rdr_env tcg
350-
351- case lookupOccEnv rdrenv occ of
352- Nothing -> empty
353- Just elts -> do
354- mvar <- lift $ lookupName (hscEnv hscenv) modul $ gre_name $ head elts
355- case mvar of
356- Just tt -> liftMaybe $ f tt
357- _ -> empty
358-
359-
360326liftMaybe :: Monad m => Maybe a -> MaybeT m a
361327liftMaybe a = MaybeT $ pure a
362328
@@ -396,3 +362,34 @@ expandTyFam :: Context -> Type -> Type
396362expandTyFam ctx = snd . normaliseType (ctxFamInstEnvs ctx) Nominal
397363
398364
365+ ------------------------------------------------------------------------------
366+ -- | Like 'tcUnifyTy', but takes a list of skolems to prevent unification of.
367+ tryUnifyUnivarsButNotSkolems :: Set TyVar -> CType -> CType -> Maybe TCvSubst
368+ tryUnifyUnivarsButNotSkolems skolems goal inst =
369+ case tcUnifyTysFG
370+ (bool BindMe Skolem . flip S. member skolems)
371+ [unCType inst]
372+ [unCType goal] of
373+ Unifiable subst -> pure subst
374+ _ -> Nothing
375+
376+
377+ updateSubst :: TCvSubst -> TacticState -> TacticState
378+ updateSubst subst s = s { ts_unifier = unionTCvSubst subst (ts_unifier s) }
379+
380+
381+ ------------------------------------------------------------------------------
382+ -- | Get the class methods of a 'PredType', correctly dealing with
383+ -- instantiation of quantified class types.
384+ methodHypothesis :: PredType -> Maybe [HyInfo CType ]
385+ methodHypothesis ty = do
386+ (tc, apps) <- splitTyConApp_maybe ty
387+ cls <- tyConClass_maybe tc
388+ let methods = classMethods cls
389+ tvs = classTyVars cls
390+ subst = zipTvSubst tvs apps
391+ pure $ methods <&> \ method ->
392+ let (_, _, ty) = tcSplitSigmaTy $ idType method
393+ in ( HyInfo (occName method) (ClassMethodPrv $ Uniquely cls) $ CType $ substTy subst ty
394+ )
395+
0 commit comments