4
4
module Wingman.GHC where
5
5
6
6
import Bag (bagToList )
7
+ import Class (classTyVars )
7
8
import ConLike
8
- import Control.Applicative (empty )
9
9
import Control.Monad.State
10
10
import Control.Monad.Trans.Maybe (MaybeT (.. ))
11
11
import CoreUtils (exprType )
12
+ import Data.Bool (bool )
12
13
import Data.Function (on )
13
14
import Data.Functor ((<&>) )
14
15
import Data.List (isPrefixOf )
@@ -18,22 +19,21 @@ import Data.Set (Set)
18
19
import qualified Data.Set as S
19
20
import Data.Traversable
20
21
import DataCon
21
- import Development.IDE (HscEnvEq (hscEnv ))
22
- import Development.IDE.Core.Compile (lookupName )
23
22
import Development.IDE.GHC.Compat hiding (exprType )
24
23
import DsExpr (dsExpr )
25
24
import DsMonad (initDs )
26
25
import FamInst (tcLookupDataFamInst_maybe )
27
26
import FamInstEnv (normaliseType )
28
27
import GHC.SourceGen (lambda )
29
28
import Generics.SYB (Data , everything , everywhere , listify , mkQ , mkT )
30
- import GhcPlugins (extractModule , GlobalRdrElt ( gre_name ), Role (Nominal ))
29
+ import GhcPlugins (Role (Nominal ))
31
30
import OccName
32
31
import TcRnMonad
33
32
import TcType
34
33
import TyCoRep
35
34
import Type
36
35
import TysWiredIn (charTyCon , doubleTyCon , floatTyCon , intTyCon )
36
+ import Unify
37
37
import Unique
38
38
import Var
39
39
import Wingman.Types
@@ -323,40 +323,6 @@ unXPat (XPat (L _ pat)) = unXPat pat
323
323
unXPat pat = pat
324
324
325
325
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
-
360
326
liftMaybe :: Monad m => Maybe a -> MaybeT m a
361
327
liftMaybe a = MaybeT $ pure a
362
328
@@ -396,3 +362,34 @@ expandTyFam :: Context -> Type -> Type
396
362
expandTyFam ctx = snd . normaliseType (ctxFamInstEnvs ctx) Nominal
397
363
398
364
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