Skip to content

Commit

Permalink
Fix codes for ghc-9.6
Browse files Browse the repository at this point in the history
  • Loading branch information
junjihashimoto committed Apr 21, 2024
1 parent adbed1d commit 54d0871
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
118 changes: 66 additions & 52 deletions src/TypeLevel/Rewrite.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,19 @@ import Data.Foldable
import Data.Traversable

-- GHC API
#if MIN_VERSION_ghc(9,0,0)
#if MIN_VERSION_ghc(9,6,0)
import GHC.Core.Coercion (Role(Representational), mkUnivCo)
import GHC.Tc.Types.Constraint (CtEvidence(ctev_loc), Ct, ctEvExpr, ctLoc, mkNonCanonical)
import GHC.Plugins (PredType, SDoc, fsep, ppr)
import GHC.Tc.Utils.TcType (eqType)
import GHC.Plugins (Plugin(pluginRecompile, tcPlugin), CommandLineOption, defaultPlugin, purePlugin)
import GHC.Tc.Types.Evidence (EvExpr, EvTerm, evCast)
import GHC.Tc.Plugin (newWanted)
import GHC.Core.TyCo.Rep (UnivCoProvenance(PluginProv))
import GHC.Plugins (synTyConDefn_maybe)
import GHC.Tc.Types (TcPluginSolveResult(..), TcPluginM, ErrCtxt, pushErrCtxtSameOrigin, TcPlugin(..), TcPluginSolver)
import GHC.Types.Unique.FM ( emptyUFM )
#elif MIN_VERSION_ghc(9,0,0)
import GHC.Core.Coercion (Role(Representational), mkUnivCo)
import GHC.Tc.Types.Constraint (CtEvidence(ctev_loc), Ct, ctEvExpr, ctLoc, mkNonCanonical)
import GHC.Plugins (PredType, SDoc, eqType, fsep, ppr)
Expand Down Expand Up @@ -50,9 +62,12 @@ data ReplaceCt = ReplaceCt
, replacementConstraints :: [Ct]
}


-- See https://gitlab.haskell.org/ghc/ghc/-/commit/9d4ba36f1de7ced62e2c0c6a911411144e9a3b27
-- Change TcPluginResult to TcPluginSolveResult.
combineReplaceCts
:: [ReplaceCt]
-> TcPluginResult
-> TcPluginSolveResult
combineReplaceCts replaceCts
= TcPluginOk (fmap solvedConstraint replaceCts)
(foldMap replacementConstraints replaceCts)
Expand Down Expand Up @@ -122,6 +137,7 @@ plugin = defaultPlugin
{ tcPlugin = \args -> Just $ TcPlugin
{ tcPluginInit = lookupTypeRules args
, tcPluginSolve = solve
, tcPluginRewrite = \_ -> emptyUFM
, tcPluginStop = \_ -> pure ()
}
, pluginRecompile = purePlugin
Expand Down Expand Up @@ -155,53 +171,51 @@ newRuleInducedWanted oldCt rule newPredType = do

solve
:: [TypeRule]
-> [Ct] -- ^ Given constraints
-> [Ct] -- ^ Derived constraints
-> [Ct] -- ^ Wanted constraints
-> TcPluginM TcPluginResult
solve _ _ _ [] = do
pure $ TcPluginOk [] []
solve rules givens _ wanteds = do
typeSubst <- execWriterT $ do
for_ givens $ \given -> do
for_ (asEqualityConstraint given) $ \(lhs, rhs) -> do
-- lhs ~ rhs
-- where lhs is typically an expression and rhs is typically a variable
let var = TypeEq rhs
let val = toTypeTerm lhs
tell [(var, val)]

replaceCts <- execWriterT $ do
for_ wanteds $ \wanted -> do
-- wanted => ...
for_ (asDecomposedConstraint wanted) $ \types -> do
-- C a b c => ...

-- C a b c
let typeTerms = fmap toTypeTerm types
let predType = fromDecomposeConstraint types

for_ (applyRules typeSubst rules typeTerms) $ \(rule, typeTerms') -> do
-- C a' b' c'
let types' = fmap fromTypeTerm typeTerms'
let predType' = fromDecomposeConstraint types'

unless (eqType predType' predType) $ do
-- co :: C a' b' c' ~R C a b c
let co = mkUnivCo
(PluginProv "TypeLevel.Rewrite")
Representational
predType'
predType
evWanted' <- lift $ newRuleInducedWanted wanted rule predType'
let wanted' = mkNonCanonical evWanted'
let futureDict :: EvExpr
futureDict = ctEvExpr evWanted'
let replaceCt :: ReplaceCt
replaceCt = ReplaceCt
{ evidenceOfCorrectness = evCast futureDict co
, replacedConstraint = wanted
, replacementConstraints = [wanted']
}
tell [replaceCt]
pure $ combineReplaceCts replaceCts
-> TcPluginSolver
solve rules = \_ givens wanteds' ->
case wanteds' of
[] -> pure (TcPluginOk [] [])
wanteds -> do
typeSubst <- execWriterT $ do
for_ givens $ \given -> do
for_ (asEqualityConstraint given) $ \(lhs, rhs) -> do
-- lhs ~ rhs
-- where lhs is typically an expression and rhs is typically a variable
let var = TypeEq rhs
let val = toTypeTerm lhs
tell [(var, val)]

replaceCts <- execWriterT $ do
for_ wanteds $ \wanted -> do
-- wanted => ...
for_ (asDecomposedConstraint wanted) $ \types -> do
-- C a b c => ...

-- C a b c
let typeTerms = fmap toTypeTerm types
let predType = fromDecomposeConstraint types

for_ (applyRules typeSubst rules typeTerms) $ \(rule, typeTerms') -> do
-- C a' b' c'
let types' = fmap fromTypeTerm typeTerms'
let predType' = fromDecomposeConstraint types'

unless (eqType predType' predType) $ do
-- co :: C a' b' c' ~R C a b c
let co = mkUnivCo
(PluginProv "TypeLevel.Rewrite")
Representational
predType'
predType
evWanted' <- lift $ newRuleInducedWanted wanted rule predType'
let wanted' = mkNonCanonical evWanted'
let futureDict :: EvExpr
futureDict = ctEvExpr evWanted'
let replaceCt :: ReplaceCt
replaceCt = ReplaceCt
{ evidenceOfCorrectness = evCast futureDict co
, replacedConstraint = wanted
, replacementConstraints = [wanted']
}
tell [replaceCt]
pure $ combineReplaceCts replaceCts
8 changes: 8 additions & 0 deletions src/TypeLevel/Rewrite/Internal/Lookup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@ import TcPluginM
import TcSMonad (getDynFlags)
#endif

#if MIN_VERSION_ghc(9,6,0)
import GHC.Types.PkgQual (PkgQual(..))
#endif

lookupModule
:: String -- ^ module name
-> TcPluginM Module
lookupModule moduleNameStr = do
let moduleName :: ModuleName
moduleName = mkModuleName moduleNameStr
#if MIN_VERSION_ghc(9,6,0)
findImportedModule moduleName NoPkgQual >>= \case
#else
findImportedModule moduleName Nothing >>= \case
#endif
Found _ module_ -> do
pure module_
findResult -> do
Expand Down
3 changes: 3 additions & 0 deletions src/TypeLevel/Rewrite/Internal/TypeEq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ module TypeLevel.Rewrite.Internal.TypeEq where

import Data.Function

#if MIN_VERSION_ghc(9,6,0)

Check failure on line 5 in src/TypeLevel/Rewrite/Internal/TypeEq.hs

View workflow job for this annotation

GitHub Actions / oldest

error: unterminated #if

Check failure on line 5 in src/TypeLevel/Rewrite/Internal/TypeEq.hs

View workflow job for this annotation

GitHub Actions / stable

error: unterminated #if

Check failure on line 5 in src/TypeLevel/Rewrite/Internal/TypeEq.hs

View workflow job for this annotation

GitHub Actions / stable

error: unterminated #if

Check failure on line 5 in src/TypeLevel/Rewrite/Internal/TypeEq.hs

View workflow job for this annotation

GitHub Actions / newest

error: unterminated #if

Check failure on line 5 in src/TypeLevel/Rewrite/Internal/TypeEq.hs

View workflow job for this annotation

GitHub Actions / newest

error: unterminated #if
import GHC.Plugins (Type)
import GHC.Tc.Utils.TcType (eqType)
#if MIN_VERSION_ghc(9,0,0)
import GHC.Plugins (Type, eqType)
#else
Expand Down

0 comments on commit 54d0871

Please sign in to comment.