diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8393352..12917e3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,14 +33,10 @@ jobs: os: [ubuntu-latest] cabal: ["latest"] ghc: - - "8.6.5" - - "8.10.7" - - "9.0.2" - - "9.2.8" - - "9.4.8" - - "9.6.4" - - "9.8.2" - - "9.10.1" + - "9.6.7" + - "9.8.4" + - "9.10.3" + - "9.12.4" steps: - uses: actions/checkout@v3 diff --git a/circuit-notation.cabal b/circuit-notation.cabal index 41972c0..c0e38f5 100644 --- a/circuit-notation.cabal +++ b/circuit-notation.cabal @@ -1,25 +1,25 @@ --- cabal-version: 2.2 +cabal-version: 2.4 name: circuit-notation version: 0.1.0.0 synopsis: A source plugin for manipulating circuits in clash with a arrow notation -- description: -license: BSD3 +license: BSD-3-Clause license-file: LICENSE author: Christopher Chalmers maintainer: c.chalmers@me.com copyright: 2024 Christopher Chalmers category: Hardware build-type: Simple -cabal-version: >=1.10 +tested-with: + GHC ==9.6.7 + , GHC ==9.8.4 + , GHC ==9.10.3 + , GHC ==9.12.4 library exposed-modules: CircuitNotation Circuit - if impl(ghc < 9.2) - other-modules: - GHC.Types.Unique.Map - - if impl(ghc < 9.10) + if impl(ghc < 9.8) other-modules: GHC.Types.Unique.Map.Extra @@ -29,7 +29,7 @@ library , clash-prelude >= 1.0 , containers , data-default - , ghc (>=8.6 && <8.8) || (>=8.10 && < 9.12) + , ghc >= 9.6 && < 9.13 , lens , mtl , parsec diff --git a/example/Example.hs b/example/Example.hs index 1c35079..6c87fe7 100644 --- a/example/Example.hs +++ b/example/Example.hs @@ -11,17 +11,12 @@ This file contains examples of using the Circuit Notation. -} {-# LANGUAGE BlockArguments #-} -{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE TypeOperators #-} -#if __GLASGOW_HASKELL__ < 810 -{-# LANGUAGE Arrows #-} -#endif - {-# OPTIONS -fplugin=CircuitNotation #-} {-# OPTIONS -fplugin-opt=CircuitNotation:debug #-} {-# OPTIONS -Wall #-} @@ -42,11 +37,6 @@ import Clash.Prelude idCircuit :: Circuit a a idCircuit = idC -#if __GLASGOW_HASKELL__ < 810 -swapC0 :: Circuit (a,b) (b,a) -swapC0 = id $ circuit $ \ ~(a,b) -> ~(b,a) -#endif - swapC1 :: Circuit (a,b) (b,a) swapC1 = id $ circuit $ \ ~(a,b) -> (b,a) diff --git a/example/Testing.hs b/example/Testing.hs index b8c8535..6ba4bbe 100644 --- a/example/Testing.hs +++ b/example/Testing.hs @@ -10,10 +10,6 @@ For testing the circuit notation. -} -{-# LANGUAGE CPP #-} -#if __GLASGOW_HASKELL__ < 810 -{-# LANGUAGE Arrows #-} -#endif {-# LANGUAGE BlockArguments #-} {-# LANGUAGE DataKinds#-} {-# LANGUAGE GADTs #-} diff --git a/flake.lock b/flake.lock index 05c26e1..b7aff61 100644 --- a/flake.lock +++ b/flake.lock @@ -4,18 +4,18 @@ "inputs": { "flake-compat": "flake-compat", "flake-utils": "flake-utils", - "ghc-tcplugins-extra": "ghc-tcplugins-extra", + "ghc-tcplugin-api": "ghc-tcplugin-api", "ghc-typelits-extra": "ghc-typelits-extra", "ghc-typelits-knownnat": "ghc-typelits-knownnat", "ghc-typelits-natnormalise": "ghc-typelits-natnormalise", "nixpkgs": "nixpkgs" }, "locked": { - "lastModified": 1754301255, - "narHash": "sha256-dne2oWxOEosMYumUZZhc3c8NyJMdiqpb/xvd1saTp30=", + "lastModified": 1776236960, + "narHash": "sha256-8I7EcvSuA7InNAms9Af+RrdWXjWee7d2jA3mhtO9p34=", "owner": "clash-lang", "repo": "clash-compiler", - "rev": "6a0810496560e2ff2a0071f315afb573c12bba39", + "rev": "7ba3e48aec6893918a447a3dd0d4ae5c5b74ee88", "type": "github" }, "original": { @@ -27,11 +27,11 @@ "flake-compat": { "flake": false, "locked": { - "lastModified": 1696426674, - "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "lastModified": 1767039857, + "narHash": "sha256-vNpUSpF5Nuw8xvDLj2KCwwksIbjua2LZCqhV1LNRDns=", "owner": "edolstra", "repo": "flake-compat", - "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "rev": "5edf11c44bc78a0d334f6334cdaf7d60d732daab", "type": "github" }, "original": { @@ -45,11 +45,11 @@ "systems": "systems" }, "locked": { - "lastModified": 1726560853, - "narHash": "sha256-X6rJYSESBVr3hBoH0WbKE5KvhPU5bloyZ2L4K60/fPQ=", + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", "owner": "numtide", "repo": "flake-utils", - "rev": "c1dfcf08411b08f6b8615f7d8971a2bfa81d5e8a", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", "type": "github" }, "original": { @@ -75,30 +75,30 @@ "type": "indirect" } }, - "ghc-tcplugins-extra": { + "ghc-tcplugin-api": { "flake": false, "locked": { - "lastModified": 1716385093, - "narHash": "sha256-pXQoPP22TicWFwpWub9CX1J+rpOKfyX2IyzlCg1qG84=", - "owner": "clash-lang", - "repo": "ghc-tcplugins-extra", - "rev": "702dda2095c66c4f5148a749c8b7dbcc8a09f5c1", + "lastModified": 1768388673, + "narHash": "sha256-3RriTela4iwbvHhF3UigmBOfxJv0+YQGAlXQbnXsX74=", + "owner": "sheaf", + "repo": "ghc-tcplugin-api", + "rev": "c583750b5899846cb455f3fe2d58b3ba9bc910d0", "type": "github" }, "original": { - "owner": "clash-lang", - "repo": "ghc-tcplugins-extra", + "owner": "sheaf", + "repo": "ghc-tcplugin-api", "type": "github" } }, "ghc-typelits-extra": { "flake": false, "locked": { - "lastModified": 1716411282, - "narHash": "sha256-YH03Ce+TEWKHGAm7BhynitZomfpYeKpqvZAviw8yEPA=", + "lastModified": 1773994244, + "narHash": "sha256-BPOTj6Dqv2Nt+C47MCayXzItXRf3HC4f2kV7lnAjAEY=", "owner": "clash-lang", "repo": "ghc-typelits-extra", - "rev": "4dadc824a3ef9a511fcf6605167715a5a655ba0d", + "rev": "1d38c49f456406937c5e9e3b3e382af6ccbff0e1", "type": "github" }, "original": { @@ -110,11 +110,11 @@ "ghc-typelits-knownnat": { "flake": false, "locked": { - "lastModified": 1716408841, - "narHash": "sha256-A2v6GkMtSJqZXpTwWfIcwshieyRySeR1bP+NogUHNoo=", + "lastModified": 1774000279, + "narHash": "sha256-41mjxMsWHXmAS2bxYVsE3OOav3sWnwPbpP56aRDVUNU=", "owner": "clash-lang", "repo": "ghc-typelits-knownnat", - "rev": "2e57de3b709dab085fb1657cf73d4f5e833229ee", + "rev": "21fb8294266484b2e267f3850f5437e87d8b241f", "type": "github" }, "original": { @@ -126,11 +126,11 @@ "ghc-typelits-natnormalise": { "flake": false, "locked": { - "lastModified": 1716387676, - "narHash": "sha256-G5p0NUy4CpjxGO1VNhb38fhkXESFPxGaZJM0qd6L74U=", + "lastModified": 1773940027, + "narHash": "sha256-MGdoDIOWAHk9IERWtGQhxB5AlBAVbmtCUouKJUv122g=", "owner": "clash-lang", "repo": "ghc-typelits-natnormalise", - "rev": "84f500a9735675e96253181939c3473a567f6f7a", + "rev": "5adf333887f5f9f00e47f69368a2b5e9705c9cbc", "type": "github" }, "original": { @@ -141,11 +141,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1727089097, - "narHash": "sha256-ZMHMThPsthhUREwDebXw7GX45bJnBCVbfnH1g5iuSPc=", + "lastModified": 1775888245, + "narHash": "sha256-nwASzrRDD1JBEu/o8ekKYEXm/oJW6EMCzCRdrwcLe90=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "568bfef547c14ca438c56a0bece08b8bb2b71a9c", + "rev": "13043924aaa7375ce482ebe2494338e058282925", "type": "github" }, "original": { diff --git a/src/Circuit.hs b/src/Circuit.hs index 0d3024d..871a481 100644 --- a/src/Circuit.hs +++ b/src/Circuit.hs @@ -10,7 +10,6 @@ This file contains the 'Circuit' type, that the notation describes. -} -{-# LANGUAGE CPP #-} {-# LANGUAGE DataKinds #-} {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE GADTs #-} @@ -28,7 +27,6 @@ module Circuit where import Clash.Prelude -#if __GLASGOW_HASKELL__ > 900 -- | Unsafe version of ':>'. Will fail if applied to empty vectors. This is used to -- workaround spurious incomplete pattern match warnings generated in newer GHC -- versions. @@ -36,7 +34,6 @@ pattern (:>!) :: a -> Vec n a -> Vec (n + 1) a pattern (:>!) x xs <- (\ys -> (head ys, tail ys) -> (x,xs)) {-# COMPLETE (:>!) #-} infixr 5 :>! -#endif type family Fwd a type family Bwd a diff --git a/src/CircuitNotation.hs b/src/CircuitNotation.hs index 62104fe..de1220a 100644 --- a/src/CircuitNotation.hs +++ b/src/CircuitNotation.hs @@ -27,8 +27,6 @@ Notation for describing the 'Circuit' type. {-# LANGUAGE ViewPatterns #-} {-# OPTIONS_GHC -Wno-unused-top-binds #-} - --- TODO: Fix warnings introduced by GHC 9.2 {-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-} module CircuitNotation @@ -44,10 +42,6 @@ import Control.Exception import qualified Data.Data as Data import Data.Default import Data.Maybe (fromMaybe) -#if __GLASGOW_HASKELL__ >= 900 -#else -import SrcLoc hiding (noLoc) -#endif import System.IO.Unsafe import Data.Typeable @@ -55,33 +49,14 @@ import Data.Typeable import qualified Language.Haskell.TH as TH import qualified GHC -#if __GLASGOW_HASKELL__ >= 902 import GHC.Types.SourceError (throwOneError) import qualified GHC.Driver.Env as GHC import qualified GHC.Types.SourceText as GHC import qualified GHC.Types.SourceError as GHC import qualified GHC.Driver.Ppr as GHC -#elif __GLASGOW_HASKELL__ >= 900 -import GHC.Driver.Types (throwOneError) -import qualified GHC.Driver.Types as GHC -#else -import HscTypes (throwOneError) -#endif - -#if __GLASGOW_HASKELL__ == 900 -import qualified GHC.Parser.Annotation as GHC -#endif - -#if __GLASGOW_HASKELL__ >= 910 -import GHC.Hs (EpAnn) -#endif -#if __GLASGOW_HASKELL__ >= 900 import GHC.Data.Bag import GHC.Data.FastString (mkFastString, unpackFS) -#if __GLASGOW_HASKELL__ < 906 -import GHC.Plugins (PromotionFlag(NotPromoted)) -#endif import GHC.Types.SrcLoc hiding (noLoc) import qualified GHC.Data.FastString as GHC import qualified GHC.Driver.Plugins as GHC @@ -92,50 +67,25 @@ import qualified GHC.Types.Name.Reader as GHC import qualified GHC.Utils.Error as Err import qualified GHC.Utils.Outputable as GHC import qualified GHC.Utils.Outputable as Outputable -#else -import Bag -import qualified ErrUtils as Err -import FastString (mkFastString, unpackFS) -import qualified GhcPlugins as GHC -import qualified OccName -import qualified Outputable -#endif -#if __GLASGOW_HASKELL__ >= 904 import GHC.Driver.Errors.Ppr () -- instance Diagnostic GhcMessage import qualified GHC.Driver.Config.Diagnostic as GHC import qualified GHC.Driver.Errors.Types as GHC import qualified GHC.Utils.Logger as GHC +#if __GLASGOW_HASKELL__ < 910 import qualified GHC.Parser.PostProcess as GHC +#else +import GHC.Parser.PostProcess () -- instances #endif -#if __GLASGOW_HASKELL__ > 808 import qualified GHC.ThToHs as Convert import GHC.Hs -#if __GLASGOW_HASKELL__ >= 902 hiding (locA) -#endif -#else -import qualified Convert -import HsSyn hiding (noExt) -import HsExtension (GhcPs, NoExt (..)) -#endif -#if __GLASGOW_HASKELL__ <= 806 -import PrelNames (eqTyCon_RDR) -#elif __GLASGOW_HASKELL__ <= 810 -import TysWiredIn (eqTyCon_RDR) -import BasicTypes (PromotionFlag( NotPromoted )) -#else import GHC.Builtin.Types (eqTyCon_RDR) -#endif -#if __GLASGOW_HASKELL__ >= 902 import "ghc" GHC.Types.Unique.Map -#else -import GHC.Types.Unique.Map -#endif #if __GLASGOW_HASKELL__ < 908 import GHC.Types.Unique.Map.Extra @@ -149,11 +99,8 @@ import qualified Control.Lens as L import Control.Lens.Operators -- mtl -import Control.Monad.State - -#if __GLASGOW_HASKELL__ >= 906 import Control.Monad -#endif +import Control.Monad.State -- pretty-show -- import qualified Text.Show.Pretty as SP @@ -192,23 +139,7 @@ imap :: (Int -> a -> b) -> [a] -> [b] imap f = zipWith f [0 ..] -- Utils for backwards compat ------------------------------------------ -#if __GLASGOW_HASKELL__ < 902 -type MsgDoc = Err.MsgDoc -type SrcSpanAnnA = SrcSpan -type SrcSpanAnnL = SrcSpan - -noSrcSpanA :: SrcSpan -noSrcSpanA = noSrcSpan - -noAnnSortKey :: NoExtField -noAnnSortKey = noExtField - -emptyComments :: NoExtField -emptyComments = noExtField - -locA :: a -> a -locA = id -#elif __GLASGOW_HASKELL__ < 910 +#if __GLASGOW_HASKELL__ < 910 type MsgDoc = Outputable.SDoc locA :: SrcAnn a -> SrcSpan @@ -226,51 +157,20 @@ noAnnSortKey :: AnnSortKey a noAnnSortKey = NoAnnSortKey #endif -#if __GLASGOW_HASKELL__ < 902 -type ErrMsg = Err.ErrMsg -#elif __GLASGOW_HASKELL__ >= 902 && __GLASGOW_HASKELL__ < 904 -type ErrMsg = Err.MsgEnvelope Err.DecoratedSDoc -#else type ErrMsg = Err.MsgEnvelope GHC.GhcMessage -#endif -#if __GLASGOW_HASKELL__ < 904 -sevFatal :: Err.Severity -sevFatal = Err.SevFatal -#else sevFatal :: Err.MessageClass sevFatal = Err.MCFatal -#endif #if __GLASGOW_HASKELL__ >= 910 noExt :: NoAnn a => a noExt = noAnn - -instance NoAnn NoExtField where - noAnn = noExtField -#elif __GLASGOW_HASKELL__ > 900 +#else noExt :: EpAnn ann noExt = EpAnnNotUsed -#elif __GLASGOW_HASKELL__ > 808 -noExt :: NoExtField -noExt = noExtField -#else -noExt :: NoExt -noExt = NoExt - -noExtField :: NoExt -noExtField = NoExt - -type NoExtField = NoExt #endif -#if __GLASGOW_HASKELL__ < 904 -pattern HsParP :: LHsExpr p -> HsExpr p -pattern HsParP e <- HsPar _ e - -pattern ParPatP :: LPat p -> Pat p -pattern ParPatP p <- ParPat _ p -#elif __GLASGOW_HASKELL__ < 910 +#if __GLASGOW_HASKELL__ < 910 pattern HsParP :: LHsExpr p -> HsExpr p pattern HsParP e <- HsPar _ _ e _ @@ -284,18 +184,9 @@ pattern ParPatP :: LPat p -> Pat p pattern ParPatP p <- ParPat _ p #endif -#if __GLASGOW_HASKELL__ < 906 -type PrintUnqualified = Outputable.PrintUnqualified -#else type PrintUnqualified = Outputable.NamePprCtx -#endif mkErrMsg :: GHC.DynFlags -> SrcSpan -> PrintUnqualified -> Outputable.SDoc -> ErrMsg -#if __GLASGOW_HASKELL__ < 902 -mkErrMsg = Err.mkErrMsg -#elif __GLASGOW_HASKELL__ >= 902 && __GLASGOW_HASKELL__ < 904 -mkErrMsg _ = Err.mkMsgEnvelope -#else -- Check the documentation of -- `GHC.Driver.Errors.Types.ghcUnkownMessage` for some background on -- why plugins should use this generic message constructor. @@ -303,19 +194,12 @@ mkErrMsg _ locn unqual = Err.mkErrorMsgEnvelope locn unqual . GHC.ghcUnknownMessage . Err.mkPlainError Err.noHints -#endif mkLongErrMsg :: GHC.DynFlags -> SrcSpan -> PrintUnqualified -> Outputable.SDoc -> Outputable.SDoc -> ErrMsg -#if __GLASGOW_HASKELL__ < 902 -mkLongErrMsg = Err.mkLongErrMsg -#elif __GLASGOW_HASKELL__ >= 902 && __GLASGOW_HASKELL__ < 904 -mkLongErrMsg _ = Err.mkLongMsgEnvelope -#else mkLongErrMsg _ locn unqual msg extra = Err.mkErrorMsgEnvelope locn unqual $ GHC.ghcUnknownMessage $ Err.mkDecoratedError Err.noHints [msg, extra] -#endif -- Types --------------------------------------------------------------- @@ -413,24 +297,11 @@ runCircuitM (CircuitM m) = do } (a, s) <- runStateT m emptyCircuitState let errs = _cErrors s -#if __GLASGOW_HASKELL__ < 904 - unless (isEmptyBag errs) $ liftIO . throwIO $ GHC.mkSrcErr errs -#else unless (isEmptyBag errs) $ liftIO . throwIO $ GHC.mkSrcErr $ Err.mkMessages errs -#endif pure a -#if __GLASGOW_HASKELL__ < 904 -mkLocMessage :: Err.Severity -> SrcSpan -> Outputable.SDoc -> Outputable.SDoc -#else mkLocMessage :: Err.MessageClass -> SrcSpan -> Outputable.SDoc -> Outputable.SDoc -#endif - -#if __GLASGOW_HASKELL__ < 906 -mkLocMessage = Err.mkLocMessageAnn Nothing -#else mkLocMessage = Err.mkLocMessage -#endif errM :: SrcSpan -> String -> CircuitM () errM loc msg = do @@ -443,16 +314,8 @@ errM loc msg = do -- It's very possible that most of these are already in the ghc library in some form. It's not the -- easiest library to discover these kind of functions. -#if __GLASGOW_HASKELL__ >= 902 conPatIn :: GenLocated SrcSpanAnnN GHC.RdrName -> HsConPatDetails GhcPs -> Pat GhcPs -#else -conPatIn :: Located GHC.RdrName -> HsConPatDetails GhcPs -> Pat GhcPs -#endif -#if __GLASGOW_HASKELL__ >= 900 conPatIn loc con = ConPat noExt loc con -#else -conPatIn loc con = ConPatIn loc con -#endif #if __GLASGOW_HASKELL__ >= 910 noEpAnn :: NoAnn ann => GenLocated SrcSpan e -> GenLocated (EpAnn ann) e @@ -460,15 +323,12 @@ noEpAnn (L l e) = L (EpAnn (spanAsAnchor l) noAnn emptyComments) e noLoc :: NoAnn ann => e -> GenLocated (EpAnn ann) e noLoc = noEpAnn . GHC.noLoc -#elif __GLASGOW_HASKELL__ >= 902 +#else noEpAnn :: GenLocated SrcSpan e -> GenLocated (SrcAnn ann) e noEpAnn (L l e) = L (SrcSpanAnn noExt l) e noLoc :: e -> GenLocated (SrcAnn ann) e noLoc = noEpAnn . GHC.noLoc -#else -noLoc :: e -> Located e -noLoc = GHC.noLoc #endif tupP :: p ~ GhcPs => [LPat p] -> LPat p @@ -478,10 +338,7 @@ tupP pats = noLoc $ TuplePat noExt pats GHC.Boxed vecP :: (?nms :: ExternalNames) => SrcSpanAnnA -> [LPat GhcPs] -> LPat GhcPs vecP srcLoc = \case [] -> go [] -#if __GLASGOW_HASKELL__ < 904 - as -> L srcLoc $ ParPat noExt $ go as - where -#elif __GLASGOW_HASKELL__ < 910 +#if __GLASGOW_HASKELL__ < 910 as -> L srcLoc $ ParPat noExt pL (go as) pR where pL = L (GHC.mkTokenLocation $ locA srcLoc) HsTok @@ -494,12 +351,7 @@ vecP srcLoc = \case #endif go :: [LPat GhcPs] -> LPat GhcPs go (p@(L l0 _):pats) = - let -#if __GLASGOW_HASKELL__ >= 902 - l1 = l0 `seq` noSrcSpanA -#else - l1 = l0 -#endif + let l1 = l0 `seq` noSrcSpanA in L srcLoc $ conPatIn (L l1 (consPat ?nms)) (InfixCon p (go pats)) go [] = L srcLoc $ WildPat noExtField @@ -511,11 +363,7 @@ tildeP :: SrcSpanAnnA -> LPat GhcPs -> LPat GhcPs tildeP loc lpat = L loc (LazyPat noExt lpat) hsBoxedTuple :: GHC.HsTupleSort -#if __GLASGOW_HASKELL__ >= 902 hsBoxedTuple = HsBoxedOrConstraintTuple -#else -hsBoxedTuple = HsBoxedTuple -#endif tupT :: [LHsType GhcPs] -> LHsType GhcPs tupT [ty] = ty @@ -532,15 +380,19 @@ appTy :: LHsType GhcPs -> LHsType GhcPs -> LHsType GhcPs appTy a b = noLoc (HsAppTy noExtField a (parenthesizeHsType GHC.appPrec b)) appE :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -appE fun arg = L noSrcSpanA $ HsApp noExt fun (parenthesizeHsExpr GHC.appPrec arg) +appE fun arg = L noSrcSpanA $ HsApp +#if __GLASGOW_HASKELL__ >= 910 + noExtField +#else + noAnn +#endif + fun (parenthesizeHsExpr GHC.appPrec arg) varE :: SrcSpanAnnA -> GHC.RdrName -> LHsExpr GhcPs varE loc rdr = L loc (HsVar noExtField (noLoc rdr)) parenE :: LHsExpr GhcPs -> LHsExpr GhcPs -#if __GLASGOW_HASKELL__ < 904 -parenE e@(L l _) = L l (HsPar noExt e) -#elif __GLASGOW_HASKELL__ < 910 +#if __GLASGOW_HASKELL__ < 910 parenE e@(L l _) = L l (HsPar noExt pL e pR) where pL = L (GHC.mkTokenLocation $ locA l) HsTok @@ -566,18 +418,26 @@ vecE srcLoc = \case [] -> go srcLoc [] as -> parenE $ go srcLoc as where - go loc (e@(L l _):es) = L loc $ OpApp noExt e (varE l (thName '(:>))) (go loc es) + go loc (e@(L l _):es) = L loc $ OpApp +#if __GLASGOW_HASKELL__ >= 912 + noExtField +#else + noExt +#endif + e (varE l (thName '(:>))) (go loc es) go loc [] = varE loc (thName 'Nil) tupE :: p ~ GhcPs => SrcSpanAnnA -> [LHsExpr p] -> LHsExpr p tupE _ [ele] = ele tupE loc elems = L loc $ ExplicitTuple noExt tupArgs GHC.Boxed where -#if __GLASGOW_HASKELL__ >= 902 - tupArgs = map (Present noExt) elems + tupArgs = map +#if __GLASGOW_HASKELL__ >= 910 + (Present noExtField) #else - tupArgs = map (\arg@(L l _) -> L l (Present noExt arg)) elems + (Present noAnn) #endif + elems unL :: Located a -> a unL (L _ a) = a @@ -591,13 +451,7 @@ thName nm = -- | Generate a "unique" name by appending the location as a string. genLocName :: SrcSpanAnnA -> String -> String -#if __GLASGOW_HASKELL__ >= 902 genLocName (locA -> GHC.RealSrcSpan rss _) prefix = -#elif __GLASGOW_HASKELL__ >= 900 -genLocName (GHC.RealSrcSpan rss _) prefix = -#else -genLocName (GHC.RealSrcSpan rss) prefix = -#endif prefix <> "_" <> foldMap (\f -> show (f rss)) [srcSpanStartLine, srcSpanEndLine, srcSpanStartCol, srcSpanEndCol] genLocName _ prefix = prefix @@ -605,14 +459,16 @@ genLocName _ prefix = prefix -- | Extract a simple lambda into inputs and body. simpleLambda :: HsExpr GhcPs -> Maybe ([LPat GhcPs], LHsExpr GhcPs) simpleLambda expr = do -#if __GLASGOW_HASKELL__ < 906 - HsLam _ (MG _x alts _origin) <- Just expr -#elif __GLASGOW_HASKELL__ < 910 +#if __GLASGOW_HASKELL__ < 910 HsLam _ (MG _x alts) <- Just expr #else HsLam _ _ (MG _x alts) <- Just expr #endif +#if __GLASGOW_HASKELL__ >= 912 + L _ [L _ (Match _matchX _matchContext (L _ matchPats) matchGr)] <- Just alts +#else L _ [L _ (Match _matchX _matchContext matchPats matchGr)] <- Just alts +#endif GRHSs _grX grHss _grLocalBinds <- Just matchGr [L _ (GRHS _ _ body)] <- Just grHss Just (matchPats, body) @@ -630,9 +486,7 @@ letE -- ^ final `in` expressions -> LHsExpr p letE loc sigs binds expr = -#if __GLASGOW_HASKELL__ < 904 - L loc (HsLet noExt localBinds expr) -#elif __GLASGOW_HASKELL__ < 908 +#if __GLASGOW_HASKELL__ < 908 L loc (HsLet noExt tkLet localBinds tkIn expr) #elif __GLASGOW_HASKELL__ < 910 L loc (HsLet noExt tkLet localBinds tkIn expr) @@ -640,18 +494,13 @@ letE loc sigs binds expr = L loc (HsLet (tkLet,tkIn) localBinds expr) #endif where -#if __GLASGOW_HASKELL__ >= 902 localBinds :: HsLocalBinds GhcPs localBinds = HsValBinds noExt valBinds -#else - localBinds :: LHsLocalBindsLR GhcPs GhcPs - localBinds = L loc $ HsValBinds noExt valBinds -#endif #if __GLASGOW_HASKELL__ >= 910 tkLet = EpTok $ spanAsAnchor $ locA loc tkIn = EpTok $ spanAsAnchor $ locA loc -#elif __GLASGOW_HASKELL__ >= 904 +#else tkLet = L (GHC.mkTokenLocation $ locA loc) HsTok tkIn = L (GHC.mkTokenLocation $ locA loc) HsTok #endif @@ -660,7 +509,11 @@ letE loc sigs binds expr = valBinds = ValBinds noAnnSortKey hsBinds sigs hsBinds :: LHsBindsLR GhcPs GhcPs +#if __GLASGOW_HASKELL__ >= 912 + hsBinds = binds +#else hsBinds = listToBag binds +#endif -- | Simple construction of a lambda expression lamE :: [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs @@ -672,9 +525,7 @@ lamE pats expr = #endif where mg :: MatchGroup GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)) -#if __GLASGOW_HASKELL__ < 906 - mg = MG noExtField matches GHC.Generated -#elif __GLASGOW_HASKELL__ < 908 +#if __GLASGOW_HASKELL__ < 908 mg = MG GHC.Generated matches #elif __GLASGOW_HASKELL__ < 910 mg = MG (GHC.Generated GHC.DoPmc) matches @@ -682,11 +533,18 @@ lamE pats expr = mg = MG (GHC.Generated GHC.OtherExpansion GHC.DoPmc) matches #endif +#if __GLASGOW_HASKELL__ >= 912 + matches :: XRec GhcPs [LMatch GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))] + matches = noLoc [singleMatch] +#else matches :: GenLocated SrcSpanAnnL [GenLocated SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)))] matches = noLoc $ [singleMatch] +#endif singleMatch :: GenLocated SrcSpanAnnA (Match GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))) -#if __GLASGOW_HASKELL__ >= 910 +#if __GLASGOW_HASKELL__ >= 912 + singleMatch = noLoc $ Match noExtField (LamAlt LamSingle) (L (EpaSpan noSrcSpan) pats) grHss +#elif __GLASGOW_HASKELL__ >= 910 singleMatch = noLoc $ Match noExt (LamAlt LamSingle) pats grHss #else singleMatch = noLoc $ Match noExt LambdaExpr pats grHss @@ -694,19 +552,10 @@ lamE pats expr = grHss :: GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)) grHss = GRHSs emptyComments [grHs] $ -#if __GLASGOW_HASKELL__ >= 902 (EmptyLocalBinds noExtField) -#else - (noLoc (EmptyLocalBinds noExtField)) -#endif -#if __GLASGOW_HASKELL__ < 904 - grHs :: GenLocated SrcSpan (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))) - grHs = L noSrcSpan $ GRHS noExt [] expr -#else grHs :: LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)) grHs = L noSrcSpanA $ GRHS noExt [] expr -#endif -- | Kinda hacky function to get a string name for named ports. fromRdrName :: GHC.RdrName -> GHC.FastString @@ -750,15 +599,10 @@ circuitBody = \case case bod of -- special case for idC as the final statement, gives better type inferences and generates nicer -- code -#if __GLASGOW_HASKELL__ < 810 - L _ (HsArrApp _xapp (L _ (HsVar _ (L _ (GHC.Unqual occ)))) arg _ _) - | OccName.occNameString occ == "idC" -> circuitMasters .= bindMaster arg -#else L _ (OpApp _ (L _ (HsVar _ (L _ (GHC.Unqual occ)))) (L _ op) port) | isFletching op , OccName.occNameString occ == "idC" -> do circuitMasters .= bindMaster port -#endif -- Otherwise create a binding and use that as the master. This is equivalent to changing -- c -< x @@ -782,24 +626,20 @@ handleStmtM :: GenLocated SrcSpanAnnA (StmtLR GhcPs GhcPs (LHsExpr GhcPs)) -> CircuitM () handleStmtM (L loc stmt) = case stmt of -#if __GLASGOW_HASKELL__ >= 902 LetStmt _xlet letBind -> -#else - LetStmt _xlet (L _ letBind) -> -#endif -- a regular let bindings case letBind of HsValBinds _ (ValBinds _ valBinds sigs) -> do +#if __GLASGOW_HASKELL__ >= 912 + circuitLets <>= valBinds +#else circuitLets <>= bagToList valBinds +#endif circuitTypes <>= sigs _ -> errM (locA loc) ("Unhandled let statement" <> show (Data.toConstr letBind)) BodyStmt _xbody body _idr _idr' -> bodyBinding Nothing body -#if __GLASGOW_HASKELL__ >= 900 BindStmt _ bind body -> -#else - BindStmt _xbody bind body _idr _idr' -> -#endif bodyBinding (Just $ bindSlave bind) body _ -> errM (locA loc) "Unhandled stmt" @@ -809,29 +649,13 @@ bindSlave (L loc expr) = case expr of VarPat _ (L _ rdrName) -> Ref (PortName loc (fromRdrName rdrName)) TuplePat _ lpat _ -> Tuple $ fmap bindSlave lpat ParPatP lpat -> bindSlave lpat -#if __GLASGOW_HASKELL__ >= 902 ConPat _ (L _ (GHC.Unqual occ)) (PrefixCon [] [lpat]) -#elif __GLASGOW_HASKELL__ >= 900 - ConPat _ (L _ (GHC.Unqual occ)) (PrefixCon [lpat]) -#else - ConPatIn (L _ (GHC.Unqual occ)) (PrefixCon [lpat]) -#endif | OccName.occNameString occ `elem` fwdNames -> FwdPat lpat -- empty list is done as the constructor -#if __GLASGOW_HASKELL__ >= 900 ConPat _ (L _ rdr) _ -#else - ConPatIn (L _ rdr) _ -#endif | rdr == thName '[] -> Vec loc [] | rdr == thName '() -> Tuple [] -#if __GLASGOW_HASKELL__ < 810 - SigPat ty port -> PortType (hsSigWcType ty) (bindSlave port) -#elif __GLASGOW_HASKELL__ < 900 - SigPat _ port ty -> PortType (hsSigWcType ty) (bindSlave port) -#else SigPat _ port ty -> PortType (hsps_body ty) (bindSlave port) -#endif LazyPat _ lpat -> Lazy loc (bindSlave lpat) ListPat _ pats -> Vec loc (map bindSlave pats) pat -> @@ -852,29 +676,14 @@ bindMaster (L loc expr) = case expr of HsApp _xapp (L _ (HsVar _ (L _ (GHC.Unqual occ)))) sig | OccName.occNameString occ `elem` fwdNames -> FwdExpr sig ExplicitTuple _ tups _ -> let -#if __GLASGOW_HASKELL__ >= 902 vals = fmap (\(Present _ e) -> e) tups -#else - vals = fmap (\(L _ (Present _ e)) -> e) tups -#endif in Tuple $ fmap bindMaster vals -#if __GLASGOW_HASKELL__ >= 902 ExplicitList _ exprs -> -#else - ExplicitList _ _syntaxExpr exprs -> -#endif Vec loc $ fmap bindMaster exprs -#if __GLASGOW_HASKELL__ < 810 - HsArrApp _xapp (L _ (HsVar _ (L _ (GHC.Unqual occ)))) sig _ _ - | OccName.occNameString occ `elem` fwdNames -> FwdExpr sig - ExprWithTySig ty expr' -> PortType (hsSigWcType ty) (bindMaster expr') - ELazyPat _ expr' -> Lazy loc (bindMaster expr') -#else -- XXX: Untested? HsProc _ _ (L _ (HsCmdTop _ (L _ (HsCmdArrApp _xapp (L _ (HsVar _ (L _ (GHC.Unqual occ)))) sig _ _)))) | OccName.occNameString occ `elem` fwdNames -> FwdExpr sig ExprWithTySig _ expr' ty -> PortType (hsSigWcType ty) (bindMaster expr') -#endif HsParP expr' -> bindMaster expr' @@ -896,21 +705,12 @@ bodyBinding -> CircuitM () bodyBinding mInput lexpr@(L loc expr) = do case expr of -#if __GLASGOW_HASKELL__ < 810 - HsArrApp _xhsArrApp circuit port HsFirstOrderApp True -> - circuitBinds <>= [Binding - { bCircuit = circuit - , bOut = bindMaster port - , bIn = fromMaybe (Tuple []) mInput - }] -#else OpApp _ circuit (L _ op) port | isFletching op -> do circuitBinds <>= [Binding { bCircuit = circuit , bOut = bindMaster port , bIn = fromMaybe (Tuple []) mInput }] -#endif _ -> case mInput of Nothing -> errM (locA loc) "standalone expressions are not allowed (are Arrows enabled?)" @@ -984,12 +784,8 @@ bindWithSuffix dflags dir = \case PortErr loc msgdoc -> unsafePerformIO . throwOneError $ mkLongErrMsg dflags (locA loc) Outputable.alwaysQualify (Outputable.text "Unhandled bind") msgdoc Lazy loc p -> tildeP loc $ bindWithSuffix dflags dir p -#if __GLASGOW_HASKELL__ >= 902 -- XXX: propagate location FwdExpr (L _ _) -> nlWildPat -#else - FwdExpr (L l _) -> L l (WildPat noExt) -#endif FwdPat lpat -> tagP lpat PortType ty p -> tagTypeP dir ty $ bindWithSuffix dflags dir p @@ -1035,7 +831,13 @@ createInputs -> PortDescription PortName -- ^ master ports -> LHsExpr p -createInputs dir slaves masters = noLoc $ OpApp noExt s2m (varE noSrcSpanA (fwdBwdCon ?nms)) m2s +createInputs dir slaves masters = noLoc $ OpApp +#if __GLASGOW_HASKELL__ >= 912 + noExtField +#else + noExt +#endif + s2m (varE noSrcSpanA (fwdBwdCon ?nms)) m2s where m2s = expWithSuffix (revDirec dir) masters s2m = expWithSuffix dir slaves @@ -1048,30 +850,19 @@ decFromBinding dflags Binding {..} = do in patBind bindPat bod patBind :: LPat GhcPs -> LHsExpr GhcPs -> HsBind GhcPs -patBind lhs expr = -#if __GLASGOW_HASKELL__ < 906 - PatBind noExt lhs rhs ([], []) -#elif __GLASGOW_HASKELL__ < 910 +patBind lhs expr = +#if __GLASGOW_HASKELL__ < 910 PatBind noExt lhs rhs #else - PatBind noExt lhs (HsNoMultAnn noExt) rhs + PatBind noExtField lhs (HsNoMultAnn noExtField) rhs #endif where rhs :: GRHSs GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)) rhs = GRHSs emptyComments [gr] $ -#if __GLASGOW_HASKELL__ >= 902 EmptyLocalBinds noExtField -#else - noLoc (EmptyLocalBinds noExtField) -#endif -#if __GLASGOW_HASKELL__ < 904 - gr :: GenLocated SrcSpan (GRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs))) - gr = L (locA (getLoc expr)) (GRHS noExt [] expr) -#else gr :: LGRHS GhcPs (GenLocated SrcSpanAnnA (HsExpr GhcPs)) gr = L (noAnnSrcSpan (getLocA expr)) (GRHS noExt [] expr) -#endif circuitConstructor :: (?nms :: ExternalNames) => SrcSpanAnnA -> LHsExpr GhcPs circuitConstructor loc = varE loc (circuitCon ?nms) @@ -1080,13 +871,8 @@ runCircuitFun :: (?nms :: ExternalNames) => SrcSpanAnnA -> LHsExpr GhcPs runCircuitFun loc = varE loc (runCircuitName ?nms) -#if __GLASGOW_HASKELL__ < 902 -prefixCon :: [arg] -> HsConDetails arg rec -prefixCon a = PrefixCon a -#else prefixCon :: [arg] -> HsConDetails tyarg arg rec prefixCon a = PrefixCon [] a -#endif taggedBundleP :: (p ~ GhcPs, ?nms :: ExternalNames) => LPat p -> LPat p taggedBundleP a = noLoc (conPatIn (noLoc (tagBundlePat ?nms)) (prefixCon [a])) @@ -1106,23 +892,11 @@ tagTypeCon = sigPat :: (p ~ GhcPs) => SrcSpanAnnA -> LHsType GhcPs -> LPat p -> LPat p sigPat loc ty a = L loc $ -#if __GLASGOW_HASKELL__ < 810 - SigPat (HsWC noExt (HsIB noExt ty)) a -#elif __GLASGOW_HASKELL__ < 900 - SigPat noExt a (HsWC noExt (HsIB noExt ty)) -#else SigPat noExt a (HsPS noExt ty) -#endif sigE :: (?nms :: ExternalNames) => SrcSpanAnnA -> LHsType GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs sigE loc ty a = L loc $ -#if __GLASGOW_HASKELL__ < 810 - ExprWithTySig (HsWC noExt (HsIB noExt ty)) a -#elif __GLASGOW_HASKELL__ < 902 - ExprWithTySig noExt a (HsWC noExt (HsIB noExt ty)) -#else ExprWithTySig noExt a (HsWC noExtField (L loc $ HsSig noExtField (HsOuterImplicit noExtField) ty)) -#endif tagTypeP :: (p ~ GhcPs, ?nms :: ExternalNames) => Direction -> LHsType GhcPs -> LPat p -> LPat p tagTypeP dir ty @@ -1153,13 +927,9 @@ unsnoc (x:xs) = Just (x:a, b) hsFunTy :: (p ~ GhcPs) => LHsType p -> LHsType p -> HsType p hsFunTy = #if __GLASGOW_HASKELL__ >= 910 - HsFunTy noExt (HsUnrestrictedArrow noExt) -#elif __GLASGOW_HASKELL__ >= 904 - HsFunTy noExt (HsUnrestrictedArrow $ L NoTokenLoc HsNormalTok) -#elif __GLASGOW_HASKELL__ >= 900 - HsFunTy noExt (HsUnrestrictedArrow GHC.NormalSyntax) + HsFunTy noExtField (HsUnrestrictedArrow noAnn) #else - HsFunTy noExt + HsFunTy noExt (HsUnrestrictedArrow $ L NoTokenLoc HsNormalTok) #endif arrTy :: p ~ GhcPs => LHsType p -> LHsType p -> LHsType p @@ -1186,13 +956,14 @@ gatherTypes = L.traverseOf_ L.cosmos addTypes tyEq :: LHsType GhcPs -> LHsType GhcPs -> LHsType GhcPs tyEq a b = -#if __GLASGOW_HASKELL__ < 904 - noLoc $ HsOpTy noExtField a (noLoc eqTyCon_RDR) b + noLoc $ HsOpTy +#if __GLASGOW_HASKELL__ >= 912 + noExtField #else - noLoc $ HsOpTy noAnn NotPromoted a (noLoc eqTyCon_RDR) b + noExt #endif --- eqTyCon is a special name that has to be exactly correct for ghc to recognise it. In 8.6 this --- lives in PrelNames and is called eqTyCon_RDR, in later ghcs it's from TysWiredIn. + NotPromoted a (noLoc eqTyCon_RDR) b +-- eqTyCon is a special name that has to be exactly correct for ghc to recognise it. -- Final construction -------------------------------------------------- @@ -1260,13 +1031,8 @@ completeUnderscores = do transform :: (?nms :: ExternalNames) => Bool -#if __GLASGOW_HASKELL__ >= 900 && __GLASGOW_HASKELL__ < 906 - -> GHC.Located HsModule - -> GHC.Hsc (GHC.Located HsModule) -#else -> GHC.Located (HsModule GhcPs) -> GHC.Hsc (GHC.Located (HsModule GhcPs)) -#endif transform debug = SYB.everywhereM (SYB.mkM transform') where transform' :: LHsExpr GhcPs -> GHC.Hsc (LHsExpr GhcPs) @@ -1320,30 +1086,17 @@ mkPlugin nms = GHC.defaultPlugin warningMsg :: Outputable.SDoc -> GHC.Hsc () warningMsg sdoc = do dflags <- GHC.getDynFlags -#if __GLASGOW_HASKELL__ < 902 - liftIO $ Err.warningMsg dflags sdoc -#elif __GLASGOW_HASKELL__ >= 902 && __GLASGOW_HASKELL__ < 904 - logger <- GHC.getLogger - liftIO $ Err.warningMsg logger dflags sdoc -#else logger <- GHC.getLogger let diagOpts = GHC.initDiagOpts dflags mc = Err.mkMCDiagnostic diagOpts GHC.WarningWithoutFlag -#if __GLASGOW_HASKELL__ >= 906 Nothing -#endif liftIO $ GHC.logMsg logger mc noSrcSpan sdoc -#endif -- | The actual implementation. pluginImpl :: (?nms :: ExternalNames) => [GHC.CommandLineOption] -> GHC.ModSummary -> -#if __GLASGOW_HASKELL__ < 904 - GHC.HsParsedModule -> GHC.Hsc GHC.HsParsedModule -#else GHC.ParsedResult -> GHC.Hsc GHC.ParsedResult -#endif pluginImpl cliOptions _modSummary m = do -- cli options are activated by -fplugin-opt=CircuitNotation:debug debug <- case cliOptions of @@ -1353,15 +1106,10 @@ pluginImpl cliOptions _modSummary m = do warningMsg $ Outputable.text $ "CircuitNotation: unknown cli options " <> show cliOptions pure False hpm_module' <- do -#if __GLASGOW_HASKELL__ < 904 - transform debug (GHC.hpm_module m) - let module' = m { GHC.hpm_module = hpm_module' } -#else transform debug $ GHC.hpm_module $ GHC.parsedResultModule m let parsedResultModule' = (GHC.parsedResultModule m) { GHC.hpm_module = hpm_module' } module' = m { GHC.parsedResultModule = parsedResultModule' } -#endif return module' -- Debugging functions ------------------------------------------------- @@ -1404,9 +1152,5 @@ defExternalNames = ExternalNames Fwd -> GHC.Unqual (OccName.mkTcOcc "Fwd") Bwd -> GHC.Unqual (OccName.mkTcOcc "Bwd") , trivialBwd = GHC.Unqual (OccName.mkVarOcc "unitBwd") -#if __GLASGOW_HASKELL__ > 900 , consPat = GHC.Unqual (OccName.mkDataOcc ":>!") -#else - , consPat = GHC.Unqual (OccName.mkDataOcc ":>") -#endif } diff --git a/src/GHC/Types/Unique/Map.hs b/src/GHC/Types/Unique/Map.hs deleted file mode 100644 index 9bf20cd..0000000 --- a/src/GHC/Types/Unique/Map.hs +++ /dev/null @@ -1,213 +0,0 @@ -{-# LANGUAGE RoleAnnotations #-} -{-# LANGUAGE TupleSections #-} -{-# LANGUAGE DeriveDataTypeable #-} -{-# LANGUAGE DeriveFunctor #-} -{-# LANGUAGE CPP #-} -{-# OPTIONS_GHC -Wall #-} - --- Like 'UniqFM', these are maps for keys which are Uniquable. --- Unlike 'UniqFM', these maps also remember their keys, which --- makes them a much better drop in replacement for 'Data.Map.Map'. --- --- Key preservation is right-biased. -module GHC.Types.Unique.Map ( - UniqMap(..), - emptyUniqMap, - isNullUniqMap, - unitUniqMap, - listToUniqMap, - listToUniqMap_C, - addToUniqMap, - addListToUniqMap, - addToUniqMap_C, - addToUniqMap_Acc, - alterUniqMap, - addListToUniqMap_C, - adjustUniqMap, - delFromUniqMap, - delListFromUniqMap, - plusUniqMap, - plusUniqMap_C, - plusMaybeUniqMap_C, - plusUniqMapList, - minusUniqMap, - intersectUniqMap, - disjointUniqMap, - mapUniqMap, - filterUniqMap, - partitionUniqMap, - sizeUniqMap, - elemUniqMap, - lookupUniqMap, - lookupWithDefaultUniqMap, - anyUniqMap, - allUniqMap -) where - -#if __GLASGOW_HASKELL__ < 900 -import Unique -import UniqFM -import Outputable -#else -import GHC.Types.Unique.FM -import GHC.Types.Unique -import GHC.Utils.Outputable -#endif - -import Data.Semigroup as Semi ( Semigroup(..) ) -import Data.Coerce -import Data.Maybe -import Data.Data - --- | Maps indexed by 'Uniquable' keys -#if __GLASGOW_HASKELL__ < 900 -newtype UniqMap k a = UniqMap (UniqFM (k, a)) -#else -newtype UniqMap k a = UniqMap (UniqFM k (k, a)) -#endif - deriving (Data, Eq, Functor) -type role UniqMap nominal representational - -instance Semigroup (UniqMap k a) where - (<>) = plusUniqMap - -instance Monoid (UniqMap k a) where - mempty = emptyUniqMap - mappend = (Semi.<>) - -instance (Outputable k, Outputable a) => Outputable (UniqMap k a) where - ppr (UniqMap m) = - brackets $ fsep $ punctuate comma $ - [ ppr k <+> text "->" <+> ppr v - | (k, v) <- eltsUFM m ] - -liftC :: (a -> a -> a) -> (k, a) -> (k, a) -> (k, a) -liftC f (_, v) (k', v') = (k', f v v') - -emptyUniqMap :: UniqMap k a -emptyUniqMap = UniqMap emptyUFM - -isNullUniqMap :: UniqMap k a -> Bool -isNullUniqMap (UniqMap m) = isNullUFM m - -unitUniqMap :: Uniquable k => k -> a -> UniqMap k a -unitUniqMap k v = UniqMap (unitUFM k (k, v)) - -listToUniqMap :: Uniquable k => [(k,a)] -> UniqMap k a -listToUniqMap kvs = UniqMap (listToUFM [ (k,(k,v)) | (k,v) <- kvs]) - -listToUniqMap_C :: Uniquable k => (a -> a -> a) -> [(k,a)] -> UniqMap k a -listToUniqMap_C f kvs = UniqMap $ - listToUFM_C (liftC f) [ (k,(k,v)) | (k,v) <- kvs] - -addToUniqMap :: Uniquable k => UniqMap k a -> k -> a -> UniqMap k a -addToUniqMap (UniqMap m) k v = UniqMap $ addToUFM m k (k, v) - -addListToUniqMap :: Uniquable k => UniqMap k a -> [(k,a)] -> UniqMap k a -addListToUniqMap (UniqMap m) kvs = UniqMap $ - addListToUFM m [(k,(k,v)) | (k,v) <- kvs] - -addToUniqMap_C :: Uniquable k - => (a -> a -> a) - -> UniqMap k a - -> k - -> a - -> UniqMap k a -addToUniqMap_C f (UniqMap m) k v = UniqMap $ - addToUFM_C (liftC f) m k (k, v) - -addToUniqMap_Acc :: Uniquable k - => (b -> a -> a) - -> (b -> a) - -> UniqMap k a - -> k - -> b - -> UniqMap k a -addToUniqMap_Acc exi new (UniqMap m) k0 v0 = UniqMap $ - addToUFM_Acc (\b (k, v) -> (k, exi b v)) - (\b -> (k0, new b)) - m k0 v0 - -alterUniqMap :: Uniquable k - => (Maybe a -> Maybe a) - -> UniqMap k a - -> k - -> UniqMap k a -alterUniqMap f (UniqMap m) k = UniqMap $ - alterUFM (fmap (k,) . f . fmap snd) m k - -addListToUniqMap_C - :: Uniquable k - => (a -> a -> a) - -> UniqMap k a - -> [(k, a)] - -> UniqMap k a -addListToUniqMap_C f (UniqMap m) kvs = UniqMap $ - addListToUFM_C (liftC f) m - [(k,(k,v)) | (k,v) <- kvs] - -adjustUniqMap - :: Uniquable k - => (a -> a) - -> UniqMap k a - -> k - -> UniqMap k a -adjustUniqMap f (UniqMap m) k = UniqMap $ - adjustUFM (\(_,v) -> (k,f v)) m k - -delFromUniqMap :: Uniquable k => UniqMap k a -> k -> UniqMap k a -delFromUniqMap (UniqMap m) k = UniqMap $ delFromUFM m k - -delListFromUniqMap :: Uniquable k => UniqMap k a -> [k] -> UniqMap k a -delListFromUniqMap (UniqMap m) ks = UniqMap $ delListFromUFM m ks - -plusUniqMap :: UniqMap k a -> UniqMap k a -> UniqMap k a -plusUniqMap (UniqMap m1) (UniqMap m2) = UniqMap $ plusUFM m1 m2 - -plusUniqMap_C :: (a -> a -> a) -> UniqMap k a -> UniqMap k a -> UniqMap k a -plusUniqMap_C f (UniqMap m1) (UniqMap m2) = UniqMap $ - plusUFM_C (liftC f) m1 m2 - -plusMaybeUniqMap_C :: (a -> a -> Maybe a) -> UniqMap k a -> UniqMap k a -> UniqMap k a -plusMaybeUniqMap_C f (UniqMap m1) (UniqMap m2) = UniqMap $ - plusMaybeUFM_C (\(_, v) (k', v') -> fmap (k',) (f v v')) m1 m2 - -plusUniqMapList :: [UniqMap k a] -> UniqMap k a -plusUniqMapList xs = UniqMap $ plusUFMList (coerce xs) - -minusUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a -minusUniqMap (UniqMap m1) (UniqMap m2) = UniqMap $ minusUFM m1 m2 - -intersectUniqMap :: UniqMap k a -> UniqMap k b -> UniqMap k a -intersectUniqMap (UniqMap m1) (UniqMap m2) = UniqMap $ intersectUFM m1 m2 - -disjointUniqMap :: UniqMap k a -> UniqMap k b -> Bool -disjointUniqMap (UniqMap m1) (UniqMap m2) = disjointUFM m1 m2 - -mapUniqMap :: (a -> b) -> UniqMap k a -> UniqMap k b -mapUniqMap f (UniqMap m) = UniqMap $ mapUFM (fmap f) m -- (,) k instance - -filterUniqMap :: (a -> Bool) -> UniqMap k a -> UniqMap k a -filterUniqMap f (UniqMap m) = UniqMap $ filterUFM (f . snd) m - -partitionUniqMap :: (a -> Bool) -> UniqMap k a -> (UniqMap k a, UniqMap k a) -partitionUniqMap f (UniqMap m) = - coerce $ partitionUFM (f . snd) m - -sizeUniqMap :: UniqMap k a -> Int -sizeUniqMap (UniqMap m) = sizeUFM m - -elemUniqMap :: Uniquable k => k -> UniqMap k a -> Bool -elemUniqMap k (UniqMap m) = elemUFM k m - -lookupUniqMap :: Uniquable k => UniqMap k a -> k -> Maybe a -lookupUniqMap (UniqMap m) k = fmap snd (lookupUFM m k) - -lookupWithDefaultUniqMap :: Uniquable k => UniqMap k a -> a -> k -> a -lookupWithDefaultUniqMap (UniqMap m) a k = fromMaybe a (fmap snd (lookupUFM m k)) - -anyUniqMap :: (a -> Bool) -> UniqMap k a -> Bool -anyUniqMap f (UniqMap m) = anyUFM (f . snd) m - -allUniqMap :: (a -> Bool) -> UniqMap k a -> Bool -allUniqMap f (UniqMap m) = allUFM (f . snd) m diff --git a/src/GHC/Types/Unique/Map/Extra.hs b/src/GHC/Types/Unique/Map/Extra.hs index c3d0ca7..e75d826 100644 --- a/src/GHC/Types/Unique/Map/Extra.hs +++ b/src/GHC/Types/Unique/Map/Extra.hs @@ -1,19 +1,9 @@ -{-# LANGUAGE CPP #-} {-# LANGUAGE PackageImports #-} module GHC.Types.Unique.Map.Extra where -#if __GLASGOW_HASKELL__ >= 902 import "ghc" GHC.Types.Unique.Map -#else -import GHC.Types.Unique.Map -#endif - -#if __GLASGOW_HASKELL__ >= 900 import GHC.Types.Unique.FM (nonDetEltsUFM) -#elif __GLASGOW_HASKELL__ <= 810 -import UniqFM (nonDetEltsUFM) -#endif nonDetUniqMapToList :: UniqMap key a -> [(key, a)] nonDetUniqMapToList (UniqMap u) = nonDetEltsUFM u