[Git][ghc/ghc][wip/T21623] Move RoughMatch code out of Unify into RoughMatch

Simon Peyton Jones (@simonpj) gitlab at gitlab.haskell.org
Fri Oct 21 12:29:46 UTC 2022



Simon Peyton Jones pushed to branch wip/T21623 at Glasgow Haskell Compiler / GHC


Commits:
925d7b49 by Simon Peyton Jones at 2022-10-21T13:31:20+01:00
Move RoughMatch code out of Unify into RoughMatch

- - - - -


6 changed files:

- compiler/GHC/Core/RoughMap.hs
- compiler/GHC/Core/Unify.hs
- compiler/GHC/Iface/Make.hs
- compiler/GHC/IfaceToCore.hs
- compiler/GHC/Tc/Instance/FunDeps.hs
- compiler/GHC/Tc/Module.hs


Changes:

=====================================
compiler/GHC/Core/RoughMap.hs
=====================================
@@ -13,6 +13,9 @@ module GHC.Core.RoughMap
   , RoughMatchLookupTc(..)
   , typeToRoughMatchLookupTc
   , roughMatchTcToLookup
+  , roughMatchTcs
+  , roughMatchTcsLookup
+  , instanceCantMatch
 
     -- * RoughMap
   , RoughMap
@@ -226,6 +229,45 @@ types don't match as well.
 
 -}
 
+{- *********************************************************************
+*                                                                      *
+                Rough matching
+*                                                                      *
+********************************************************************* -}
+
+{- Note [Rough matching in class and family instances]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+Consider
+  instance C (Maybe [Tree a]) Bool
+and suppose we are looking up
+     C Bool Bool
+
+We can very quickly rule the instance out, because the first
+argument is headed by Maybe, whereas in the constraint we are looking
+up has first argument headed by Bool.  These "headed by" TyCons are
+called the "rough match TyCons" of the constraint or instance.
+They are used for a quick filter, to check when an instance cannot
+possibly match.
+
+The main motivation is to avoid sucking in whole instance
+declarations that are utterly useless.  See GHC.Core.InstEnv
+Note [ClsInst laziness and the rough-match fields].
+
+INVARIANT: a rough-match TyCons `tc` is always a real, generative tycon,
+like Maybe or Either, including a newtype or a data family, both of
+which are generative. It replies True to `isGenerativeTyCon tc Nominal`.
+
+But it is never
+    - A type synonym
+      E.g. Int and (S Bool) might match
+           if (S Bool) is a synonym for Int
+
+    - A type family (#19336)
+      E.g.   (Just a) and (F a) might match if (F a) reduces to (Just a)
+             albeit perhaps only after 'a' is instantiated.
+-}
+
+
 -- Key for insertion into a RoughMap
 data RoughMatchTc
   = RM_KnownTc Name  -- INVARIANT: Name refers to a TyCon tc that responds
@@ -247,18 +289,35 @@ instance Outputable RoughMatchLookupTc where
     ppr RML_NoKnownTc = text "RML_NoKnownTC"
     ppr RML_WildCard = text "_"
 
-roughMatchTcToLookup :: RoughMatchTc -> RoughMatchLookupTc
-roughMatchTcToLookup (RM_KnownTc n) = RML_KnownTc n
-roughMatchTcToLookup RM_WildCard = RML_WildCard
-
 instance Outputable RoughMatchTc where
     ppr (RM_KnownTc nm) = text "KnownTc" <+> ppr nm
     ppr RM_WildCard = text "OtherTc"
 
+instanceCantMatch :: [RoughMatchTc] -> [RoughMatchTc] -> Bool
+-- (instanceCantMatch tcs1 tcs2) returns True if tcs1 cannot
+-- possibly be instantiated to actual, nor vice versa;
+-- False is non-committal
+instanceCantMatch (mt : ts) (ma : as) = itemCantMatch mt ma || instanceCantMatch ts as
+instanceCantMatch _         _         =  False  -- Safe
+
+itemCantMatch :: RoughMatchTc -> RoughMatchTc -> Bool
+itemCantMatch (RM_KnownTc t) (RM_KnownTc a) = t /= a
+itemCantMatch _              _              = False
+
+roughMatchTcToLookup :: RoughMatchTc -> RoughMatchLookupTc
+roughMatchTcToLookup (RM_KnownTc n) = RML_KnownTc n
+roughMatchTcToLookup RM_WildCard = RML_WildCard
+
 isRoughWildcard :: RoughMatchTc -> Bool
 isRoughWildcard RM_WildCard  = True
 isRoughWildcard (RM_KnownTc {}) = False
 
+roughMatchTcs :: [Type] -> [RoughMatchTc]
+roughMatchTcs tys = map typeToRoughMatchTc tys
+
+roughMatchTcsLookup :: [Type] -> [RoughMatchLookupTc]
+roughMatchTcsLookup tys = map typeToRoughMatchLookupTc tys
+
 typeToRoughMatchLookupTc :: Type -> RoughMatchLookupTc
 typeToRoughMatchLookupTc ty
   | Just (ty', _) <- splitCastTy_maybe ty


=====================================
compiler/GHC/Core/Unify.hs
=====================================
@@ -10,15 +10,12 @@ module GHC.Core.Unify (
         tcMatchTyX, tcMatchTysX, tcMatchTyKisX,
         tcMatchTyX_BM, ruleMatchTyKiX,
 
-        -- * Rough matching
-        RoughMatchTc(..), roughMatchTcs, roughMatchTcsLookup, instanceCantMatch,
-        typesCantMatch, typesAreApart, isRoughWildcard,
-
         -- Side-effect free unification
         tcUnifyTy, tcUnifyTyKi, tcUnifyTys, tcUnifyTyKis,
         tcUnifyTysFG, tcUnifyTyWithTFs,
         BindFun, BindFlag(..), matchBindFun, alwaysBindFun,
         UnifyResult, UnifyResultM(..), MaybeApartReason(..),
+        typesCantMatch, typesAreApart,
 
         -- Matching a type against a lifted type (coercion)
         liftCoMatch,
@@ -41,7 +38,6 @@ import GHC.Core.TyCo.Rep
 import GHC.Core.TyCo.Compare ( eqType, tcEqType )
 import GHC.Core.TyCo.FVs     ( tyCoVarsOfCoList, tyCoFVsOfTypes )
 import GHC.Core.TyCo.Subst   ( mkTvSubst, emptyIdSubstEnv )
-import GHC.Core.RoughMap
 import GHC.Core.Map.Type
 import GHC.Utils.FV( FV, fvVarList )
 import GHC.Utils.Misc
@@ -253,62 +249,6 @@ matchBindFun tvs tv _ty
 alwaysBindFun :: BindFun
 alwaysBindFun _tv _ty = BindMe
 
-{- *********************************************************************
-*                                                                      *
-                Rough matching
-*                                                                      *
-********************************************************************* -}
-
-{- Note [Rough matching in class and family instances]
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-Consider
-  instance C (Maybe [Tree a]) Bool
-and suppose we are looking up
-     C Bool Bool
-
-We can very quickly rule the instance out, because the first
-argument is headed by Maybe, whereas in the constraint we are looking
-up has first argument headed by Bool.  These "headed by" TyCons are
-called the "rough match TyCons" of the constraint or instance.
-They are used for a quick filter, to check when an instance cannot
-possibly match.
-
-The main motivation is to avoid sucking in whole instance
-declarations that are utterly useless.  See GHC.Core.InstEnv
-Note [ClsInst laziness and the rough-match fields].
-
-INVARIANT: a rough-match TyCons `tc` is always a real, generative tycon,
-like Maybe or Either, including a newtype or a data family, both of
-which are generative. It replies True to `isGenerativeTyCon tc Nominal`.
-
-But it is never
-    - A type synonym
-      E.g. Int and (S Bool) might match
-           if (S Bool) is a synonym for Int
-
-    - A type family (#19336)
-      E.g.   (Just a) and (F a) might match if (F a) reduces to (Just a)
-             albeit perhaps only after 'a' is instantiated.
--}
-
-roughMatchTcs :: [Type] -> [RoughMatchTc]
-roughMatchTcs tys = map typeToRoughMatchTc tys
-
-roughMatchTcsLookup :: [Type] -> [RoughMatchLookupTc]
-roughMatchTcsLookup tys = map typeToRoughMatchLookupTc tys
-
-instanceCantMatch :: [RoughMatchTc] -> [RoughMatchTc] -> Bool
--- (instanceCantMatch tcs1 tcs2) returns True if tcs1 cannot
--- possibly be instantiated to actual, nor vice versa;
--- False is non-committal
-instanceCantMatch (mt : ts) (ma : as) = itemCantMatch mt ma || instanceCantMatch ts as
-instanceCantMatch _         _         =  False  -- Safe
-
-itemCantMatch :: RoughMatchTc -> RoughMatchTc -> Bool
-itemCantMatch (RM_KnownTc t) (RM_KnownTc a) = t /= a
-itemCantMatch _           _           = False
-
-
 {-
 ************************************************************************
 *                                                                      *


=====================================
compiler/GHC/Iface/Make.hs
=====================================
@@ -48,7 +48,7 @@ import GHC.Core.InstEnv
 import GHC.Core.FamInstEnv
 import GHC.Core.Ppr
 import GHC.Core.TyCo.Compare( eqTypes )
-import GHC.Core.Unify( RoughMatchTc(..) )
+import GHC.Core.RoughMap( RoughMatchTc(..) )
 
 import GHC.Driver.Config.HsToCore.Usage
 import GHC.Driver.Env


=====================================
compiler/GHC/IfaceToCore.hs
=====================================
@@ -57,7 +57,7 @@ import GHC.Core.TyCo.Subst ( substTyCoVars )
 import GHC.Core.InstEnv
 import GHC.Core.FamInstEnv
 import GHC.Core
-import GHC.Core.Unify( RoughMatchTc(..) )
+import GHC.Core.RoughMap( RoughMatchTc(..) )
 import GHC.Core.Utils
 import GHC.Core.Unfold( calcUnfoldingGuidance )
 import GHC.Core.Unfold.Make


=====================================
compiler/GHC/Tc/Instance/FunDeps.hs
=====================================
@@ -29,6 +29,7 @@ import GHC.Types.Var
 import GHC.Core.Class
 import GHC.Core.Predicate
 import GHC.Core.Type
+import GHC.Core.RoughMap( RoughMatchTc(..) )
 import GHC.Core.Coercion.Axiom( TypeEqn )
 import GHC.Core.Unify
 import GHC.Core.InstEnv


=====================================
compiler/GHC/Tc/Module.hs
=====================================
@@ -121,7 +121,7 @@ import GHC.Core.Type
 import GHC.Core.Class
 import GHC.Core.Coercion.Axiom
 import GHC.Core.Reduction ( Reduction(..) )
-import GHC.Core.Unify( RoughMatchTc(..) )
+import GHC.Core.RoughMap( RoughMatchTc(..) )
 import GHC.Core.TyCo.Compare( eqType, eqTypeX, eqVarBndrs )
 import GHC.Core.TyCo.Ppr( debugPprType )
 import GHC.Core.FamInstEnv



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/925d7b492a193049f0872e74464bceb998b53f00

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/925d7b492a193049f0872e74464bceb998b53f00
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20221021/9b45090a/attachment-0001.html>


More information about the ghc-commits mailing list