@@ -36,13 +36,32 @@ import qualified Data.Sequence as Seq
3636import qualified Data.Set as OrdSet
3737-- import qualified Data.Map (Map) as OrdMap
3838import qualified Data.Map as OrdMap
39- #if MIN_VERSION_GLASGOW_HASKELL(8,2,0,0)
40- import qualified UniqDFM as DFMap
41- #endif
4239import Text.Printf (printf )
4340import System.IO.Unsafe (unsafePerformIO )
4441import Data.IORef
4542
43+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
44+ import GHC.Builtin.Names (leftDataConName ,rightDataConName
45+ ,floatTyConKey ,doubleTyConKey ,integerTyConKey
46+ ,intTyConKey ,boolTyConKey )
47+ import GHC.Builtin.Types.Prim (intPrimTyCon )
48+ import GHC.Core.Class (classAllSelIds )
49+ -- For normaliseType etc
50+ import GHC.Core.FamInstEnv
51+ import GHC.Core.Lint (lintExpr )
52+ import GHC.Core.Opt.Arity (etaExpand )
53+ import GHC.Core.SimpleOpt (simpleOptExpr )
54+ import GHC.Core.TyCo.Rep
55+ import GHC.Core.Type (coreView )
56+ import GHC.Data.Pair (Pair (.. ))
57+ import GHC.Plugins as GHC hiding (substTy ,cat )
58+ import GHC.Runtime.Loader
59+ import GHC.Tc.Utils.TcType (isFloatTy ,isDoubleTy ,isIntegerTy ,isIntTy ,isBoolTy ,isUnitTy
60+ ,tcSplitTyConApp_maybe )
61+ import GHC.Types.Id.Make (mkDictSelRhs ,coerceId )
62+ import GHC.Types.Unique (mkBuiltinUnique )
63+ import qualified GHC.Types.Unique.DFM as DFMap
64+ #else
4665import GhcPlugins as GHC hiding (substTy ,cat )
4766import Class (classAllSelIds )
4867import CoreArity (etaExpand )
@@ -57,32 +76,55 @@ import Type (coreView)
5776import TcType (isFloatTy ,isDoubleTy ,isIntegerTy ,isIntTy ,isBoolTy ,isUnitTy
5877 ,tcSplitTyConApp_maybe )
5978import TysPrim (intPrimTyCon )
60- import FamInstEnv (normaliseType )
79+ -- For normaliseType etc
80+ import FamInstEnv
6181#if MIN_VERSION_GLASGOW_HASKELL(8,2,0,0)
6282import CoreOpt (simpleOptExpr )
83+ import qualified UniqDFM as DFMap
6384#endif
6485import TyCoRep
65- import GHC.Classes
6686import Unique (mkBuiltinUnique )
67- -- For normaliseType etc
68- import FamInstEnv
87+ #endif
88+ import GHC.Classes
6989
7090import ConCat.Misc (Unop ,Binop ,Ternop ,PseudoFun (.. ),(~>) )
7191import ConCat.BuildDictionary
7292-- import ConCat.Simplify
7393
74- -- GHC 8.10 FunTy as an extra operand
75- #if MIN_VERSION_GLASGOW_HASKELL(8,10,0,0)
76- pattern FunTy' a r <-
77- FunTy _ a r
78-
94+ pattern FunCo' :: Role -> Coercion -> Coercion -> Coercion
95+ mkFunCo' :: Role -> Coercion -> Coercion -> Coercion
96+ pattern FunTy' :: Type -> Type -> Type
7997mkFunTy' :: Type -> Type -> Type
80- mkFunTy' a b = FunTy VisArg a b
81- #else
98+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
99+ pattern FunCo' r a b <- FunCo r _ a b
100+ mkFunCo' r = FunCo r (multToCo One )
101+ pattern FunTy' a r <- FunTy _ _ a r
102+ mkFunTy' = FunTy VisArg One
103+ -- GHC 8.10 FunTy as an extra operand
104+ #elif MIN_VERSION_GLASGOW_HASKELL(8,10,0,0)
105+ pattern FunCo' r a b = FunCo r a b
106+ mkFunCo' = FunCo
107+ pattern FunTy' a r <- FunTy _ a r
108+ mkFunTy' = FunTy VisArg
109+ #elif MIN_VERSION_GLASGOW_HASKELL(8,2,0,0)
110+ pattern FunCo' r a b = FunCo r a b
111+ mkFunCo' = FunCo
82112pattern FunTy' a r = FunTy a r
113+ mkFunTy' = FunTy
114+ #else
115+ pattern FunCo' r dom ran <- TyConAppCo r (isFunTyCon -> True ) [dom,ran]
116+ where FunCo' = mkFunCo
117+ pattern FunTy' dom ran <- (splitFunTy_maybe -> Just (dom,ran))
118+ where FunTy' = mkFunTy
119+ -- TODO: Replace explicit uses of splitFunTy_maybe
120+ -- TODO: Look for other useful pattern synonyms
121+ #endif
83122
84- mkFunTy' :: Type -> Type -> Type
85- mkFunTy' a b = FunTy a b
123+ splitFunTy_maybe' :: Type -> Maybe (Type , Type )
124+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
125+ splitFunTy_maybe' = fmap (\ (_, a, b) -> (a, b)) . splitFunTy_maybe
126+ #else
127+ splitFunTy_maybe' = splitFunTy_maybe
86128#endif
87129
88130-- Information needed for reification. We construct this info in
@@ -121,8 +163,10 @@ data CccEnv = CccEnv { dtrace :: forall a. String -> SDoc -> a -> a
121163 -- , hasRepFromAbstCo :: Coercion -> CoreExpr
122164 , prePostV :: Id
123165 -- , lazyV :: Id
124- # if MIN_VERSION_GLASGOW_HASKELL (8 ,2 ,0 ,0 )
125- , boxers :: DFMap. UniqDFM {- TyCo-} Id -- to remove
166+ # if MIN_VERSION_GLASGOW_HASKELL (9 ,0 ,0 ,0 )
167+ , boxers :: DFMap. UniqDFM TyCon Id -- to remove
168+ # elif MIN_VERSION_GLASGOW_HASKELL (8 ,2 ,0 ,0 )
169+ , boxers :: DFMap. UniqDFM Id -- to remove
126170# else
127171 , boxers :: OrdMap. Map TyCon Id
128172# endif
@@ -191,7 +235,7 @@ ccc (CccEnv {..}) (Ops {..}) cat =
191235 Trying (" top flipForkT" )
192236 -- f | pprTrace "flipForkT tests"
193237 -- (ppr ( splitFunTy (exprType f)
194- -- , second splitFunTy_maybe (splitFunTy (exprType f))
238+ -- , second splitFunTy_maybe' (splitFunTy (exprType f))
195239 -- , not catClosed)) False = undefined
196240 f | z `FunTy ` (a `FunTy ` b) <- exprType f
197241 , not catClosed
@@ -269,7 +313,7 @@ ccc (CccEnv {..}) (Ops {..}) cat =
269313 -- I think GHC is undoing this transformation, so continue eagerly
270314 -- (`Cast` co') <$> go e
271315 Trying (" top const cast" )
272- Cast (Lam v e) (FunCo _r _ co'@ (coercionKind -> Pair b b'))
316+ Cast (Lam v e) (FunCo' _r _ co'@ (coercionKind -> Pair b b'))
273317 | not (v `isFreeIn` e)
274318 -- , dtrace "top const cast" (ppr (varWithType castConstTV)) True
275319 , Just mk <- onDictMaybe <=< onDictMaybe $
@@ -327,7 +371,7 @@ ccc (CccEnv {..}) (Ops {..}) cat =
327371 -- dtrace "top App result" (ppr (mkCompose cat uncU' (mkFork cat v' (mkId cat dom)))) $
328372 return (mkCompose cat uncU' (mkFork cat v' (mkId cat dom)))
329373 where
330- Just (dom,_) = splitFunTy_maybe (exprType e)
374+ Just (dom,_) = splitFunTy_maybe' (exprType e)
331375 Tick t e -> Doing (" top tick" )
332376 return $ Tick t (mkCcc e)
333377 _e -> Doing (" top Unhandled" )
@@ -590,7 +634,7 @@ ccc (CccEnv {..}) (Ops {..}) cat =
590634 co'' = downgradeRole r r' co' -- same as co?
591635 in
592636 -- pprTrace "lam nominal Cast" (ppr co $$ text "-->" $$ ppr co'') $
593- return (mkCcc (Cast (Lam x body') (FunCo r (mkReflCo r xty) co'')))
637+ return (mkCcc (Cast (Lam x body') (mkFunCo' r (mkReflCo r xty) co'')))
594638 Trying (" lam representational cast" )
595639 e@ (Cast e' _) ->
596640 Doing (" lam representational cast" )
@@ -839,7 +883,7 @@ mkOps (CccEnv {..}) guts annotations famEnvs dflags inScope evTy ev cat = Ops {.
839883 catTy (tyArgs2 -> (a,b)) = mkAppTys cat [a,b]
840884 reCatCo :: Rewrite Coercion
841885 -- reCatCo co | dtrace "reCatCo" (ppr co) False = undefined
842- reCatCo (FunCo r a b) = Just (mkAppCos (mkReflCo r cat) [a,b])
886+ reCatCo (FunCo' r a b) = Just (mkAppCos (mkReflCo r cat) [a,b])
843887 reCatCo (splitAppCo_maybe -> Just
844888 (splitAppCo_maybe -> Just
845889#if MIN_VERSION_GLASGOW_HASKELL(8,8,0,0)
@@ -890,7 +934,7 @@ mkOps (CccEnv {..}) guts annotations famEnvs dflags inScope evTy ev cat = Ops {.
890934 noDictErr doc =
891935 either (\ msg -> pprPanic " ccc - couldn't build dictionary for" (doc GHC. <> colon $$ msg)) id
892936 onDictTry :: CoreExpr -> Either SDoc CoreExpr
893- onDictTry e | Just (ty,_) <- splitFunTy_maybe (exprType e)
937+ onDictTry e | Just (ty,_) <- splitFunTy_maybe' (exprType e)
894938 , isPredTy' ty = App e <$> buildDictMaybe ty
895939 | otherwise = return e
896940 -- pprPanic "ccc / onDictTy: not a function from pred" (pprWithType e)
@@ -907,7 +951,7 @@ mkOps (CccEnv {..}) guts annotations famEnvs dflags inScope evTy ev cat = Ops {.
907951 -- Yet another variant: keep applying to dictionaries as long as we have
908952 -- a predicate type. TODO: reassess and refactor these variants.
909953 onDicts :: Unop CoreExpr
910- onDicts e | Just (ty,_) <- splitFunTy_maybe (exprType e)
954+ onDicts e | Just (ty,_) <- splitFunTy_maybe' (exprType e)
911955 , isPredTy' ty = onDicts (onDict e)
912956 | otherwise = e
913957 buildDictMaybe :: Type -> Either SDoc CoreExpr
@@ -926,7 +970,7 @@ mkOps (CccEnv {..}) guts annotations famEnvs dflags inScope evTy ev cat = Ops {.
926970 mkCcc' e = varApps cccPV [cat,a,b,evTy] [ev,e]
927971 where
928972 (a,b) = fromMaybe (pprPanic " mkCcc non-function:" (pprWithType e)) $
929- splitFunTy_maybe (exprType e)
973+ splitFunTy_maybe' (exprType e)
930974 mkCcc :: Unop CoreExpr -- Any reason to parametrize over Cat?
931975 mkCcc e = -- dtrace "mkCcc" (ppr (cat, e)) $
932976 mkCcc' e
@@ -1330,12 +1374,12 @@ composeRuleName = fsLit "compose/coerce"
13301374evidenceRuleName :: FastString
13311375evidenceRuleName = fsLit " evidence annotation"
13321376
1333- cccRules :: Maybe (IORef Int ) -> FamInstEnvs -> CccEnv -> ModGuts -> AnnEnv -> [CoreRule ]
1334- cccRules steps famEnvs env@ (CccEnv {.. }) guts annotations =
1377+ cccRules :: Maybe (IORef Int ) -> FamInstEnvs -> CccEnv -> ModGuts -> AnnEnv -> DynFlags -> [CoreRule ]
1378+ cccRules steps famEnvs env@ (CccEnv {.. }) guts annotations dflags =
13351379 [ BuiltinRule { ru_name = cccRuleName
13361380 , ru_fn = varName cccPV
13371381 , ru_nargs = 6 -- including type args
1338- , ru_try = \ dflags inScope _fn ->
1382+ , ru_try = \ _rOpts inScope _fn ->
13391383 \ case
13401384 -- _args | pprTrace "ccc ru_try args" (ppr _args) False -> undefined
13411385 _es@ (Type k : Type _a : Type _b : Type evType : ev : arg : _) ->
@@ -1405,7 +1449,7 @@ install opts todos =
14051449 let famEnvs = (pkgFamEnv, mg_fam_inst_env guts)
14061450 maxSteps = (unsafePerformIO . newIORef) <$>
14071451 parseOpt " maxSteps" opts
1408- return (on_mg_rules (++ cccRules maxSteps famEnvs env guts allAnns) guts)
1452+ return (on_mg_rules (++ cccRules maxSteps famEnvs env guts allAnns dflags ) guts)
14091453 delCccRule guts = return (on_mg_rules (filter (not . isCccRule)) guts)
14101454 isCccRule r = isBuiltinRule r && ru_name r `elem` [cccRuleName,composeRuleName]
14111455 -- isCCC r | is = pprTrace "delRule" (ppr cccRuleName) is
@@ -1561,8 +1605,13 @@ mkCccEnv opts = do
15611605 let boxers = OrdMap. fromList [(intTyCon,boxIV),(doubleTyCon,boxDV),(floatTyCon,boxFV)]
15621606#endif
15631607 -- _ <- findId "GHC.Num" "subtract" -- help the plugin find instances for Float and Double
1608+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
1609+ when (isDeadEndId cccV) $
1610+ pprPanic " isDeadEndId cccV" empty
1611+ #else
15641612 when (isBottomingId cccV) $
15651613 pprPanic " isBottomingId cccV" empty
1614+ #endif
15661615 return (CccEnv { .. })
15671616
15681617-- Variables that have associated ccc rewrite rules in AltCat. If we have
@@ -1698,7 +1747,11 @@ qualifiedName nm =
16981747-- binders, which is handy as dead binders can appear with live binders of the
16991748-- same variable.
17001749subst :: [(Id ,CoreExpr )] -> Unop CoreExpr
1750+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
1751+ subst ps = substExpr (foldr add emptySubst ps')
1752+ #else
17011753subst ps = substExpr " subst" (foldr add emptySubst ps')
1754+ #endif
17021755 where
17031756 add (v,new) sub = extendIdSubst sub v new
17041757 ps' = filter (not . isDeadBinder . fst ) ps
@@ -1756,22 +1809,6 @@ stringExpr = Lit . mkMachString
17561809varNameExpr :: Id -> CoreExpr
17571810varNameExpr = stringExpr . uniqVarName
17581811
1759- #if ! MIN_VERSION_GLASGOW_HASKELL(8,2,0,0)
1760-
1761- pattern FunTy :: Type -> Type -> Type
1762- pattern FunTy dom ran <- (splitFunTy_maybe -> Just (dom,ran))
1763- where FunTy = mkFunTy
1764-
1765- -- TODO: Replace explicit uses of splitFunTy_maybe
1766-
1767- -- TODO: Look for other useful pattern synonyms
1768-
1769- pattern FunCo :: Role -> Coercion -> Coercion -> Coercion
1770- pattern FunCo r dom ran <- TyConAppCo r (isFunTyCon -> True ) [dom,ran]
1771- where FunCo = mkFunCo
1772-
1773- #endif
1774-
17751812onCaseRhs :: Type -> Unop (Unop CoreExpr )
17761813onCaseRhs altsTy' f (Case scrut v _ alts) =
17771814 Case scrut v altsTy' (onAltRhs f <$> alts)
@@ -1858,7 +1895,11 @@ onExprHead _dflags h = (fmap.fmap) simpleOptExpr' $
18581895freshId :: VarSet -> String -> Type -> Id
18591896freshId used nm ty =
18601897 uniqAway (mkInScopeSet used) $
1898+ #if MIN_VERSION_GLASGOW_HASKELL(9,0,0,0)
1899+ mkSysLocal (fsLit nm) (mkBuiltinUnique 17 ) One ty
1900+ #else
18611901 mkSysLocal (fsLit nm) (mkBuiltinUnique 17 ) ty
1902+ #endif
18621903
18631904freshDeadId :: VarSet -> String -> Type -> Id
18641905freshDeadId used nm ty = setIdOccInfo (freshId used nm ty) IAmDead
0 commit comments