{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Distribution.Backpack.UnifyM (
UnifyM,
runUnifyM,
failWith,
addErr,
failIfErrs,
tryM,
addErrContext,
addErrContextM,
liftST,
UnifEnv(..),
getUnifEnv,
ModuleU,
ModuleU'(..),
convertModule,
convertModuleU,
UnitIdU,
UnitIdU'(..),
convertUnitId,
convertUnitIdU,
ModuleSubstU,
convertModuleSubstU,
convertModuleSubst,
ModuleScopeU,
emptyModuleScopeU,
convertModuleScopeU,
ModuleWithSourceU,
convertInclude,
convertModuleProvides,
convertModuleProvidesU,
) where
import Prelude ()
import Distribution.Compat.Prelude hiding (mod)
import Distribution.Backpack.ModuleShape
import Distribution.Backpack.ModuleScope
import Distribution.Backpack.ModSubst
import Distribution.Backpack.FullUnitId
import Distribution.Backpack
import qualified Distribution.Utils.UnionFind as UnionFind
import Distribution.ModuleName
import Distribution.Package
import Distribution.PackageDescription
import Distribution.Pretty
import Distribution.Types.ComponentInclude
import Distribution.Types.AnnotatedId
import Distribution.Verbosity
import Data.STRef
import Data.Traversable
import Control.Monad.ST
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.IntMap (IntMap)
import qualified Data.IntMap as IntMap
import Text.PrettyPrint
data ErrMsg = ErrMsg {
ErrMsg -> Doc
err_msg :: Doc,
ErrMsg -> [Doc]
err_ctx :: [Doc]
}
type MsgDoc = Doc
renderErrMsg :: ErrMsg -> MsgDoc
renderErrMsg :: ErrMsg -> Doc
renderErrMsg ErrMsg { err_msg :: ErrMsg -> Doc
err_msg = Doc
msg, err_ctx :: ErrMsg -> [Doc]
err_ctx = [Doc]
ctx } =
Doc
msg Doc -> Doc -> Doc
$$ [Doc] -> Doc
vcat [Doc]
ctx
newtype UnifyM s a = UnifyM { forall s a. UnifyM s a -> UnifEnv s -> ST s (Maybe a)
unUnifyM :: UnifEnv s -> ST s (Maybe a) }
runUnifyM :: Verbosity -> ComponentId -> FullDb -> (forall s. UnifyM s a) -> Either [MsgDoc] a
runUnifyM :: forall a.
Verbosity
-> ComponentId
-> FullDb
-> (forall s. UnifyM s a)
-> Either [Doc] a
runUnifyM Verbosity
verbosity ComponentId
self_cid FullDb
db forall s. UnifyM s a
m
= (forall s. ST s (Either [Doc] a)) -> Either [Doc] a
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Either [Doc] a)) -> Either [Doc] a)
-> (forall s. ST s (Either [Doc] a)) -> Either [Doc] a
forall a b. (a -> b) -> a -> b
$ do STRef s Int
i <- Int -> ST s (STRef s Int)
forall a s. a -> ST s (STRef s a)
newSTRef Int
0
STRef s (Map ModuleName (ModuleU s))
hmap <- Map ModuleName (ModuleU s)
-> ST s (STRef s (Map ModuleName (ModuleU s)))
forall a s. a -> ST s (STRef s a)
newSTRef Map ModuleName (ModuleU s)
forall k a. Map k a
Map.empty
STRef s [ErrMsg]
errs <- [ErrMsg] -> ST s (STRef s [ErrMsg])
forall a s. a -> ST s (STRef s a)
newSTRef []
Maybe a
mb_r <- UnifyM s a -> UnifEnv s -> ST s (Maybe a)
forall s a. UnifyM s a -> UnifEnv s -> ST s (Maybe a)
unUnifyM UnifyM s a
forall s. UnifyM s a
m UnifEnv {
unify_uniq :: STRef s Int
unify_uniq = STRef s Int
i,
unify_reqs :: STRef s (Map ModuleName (ModuleU s))
unify_reqs = STRef s (Map ModuleName (ModuleU s))
hmap,
unify_self_cid :: ComponentId
unify_self_cid = ComponentId
self_cid,
unify_verbosity :: Verbosity
unify_verbosity = Verbosity
verbosity,
unify_ctx :: [Doc]
unify_ctx = [],
unify_db :: FullDb
unify_db = FullDb
db,
unify_errs :: STRef s [ErrMsg]
unify_errs = STRef s [ErrMsg]
errs }
[ErrMsg]
final_errs <- STRef s [ErrMsg] -> ST s [ErrMsg]
forall s a. STRef s a -> ST s a
readSTRef STRef s [ErrMsg]
errs
case Maybe a
mb_r of
Just a
x | [ErrMsg] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ErrMsg]
final_errs -> Either [Doc] a -> ST s (Either [Doc] a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (a -> Either [Doc] a
forall a b. b -> Either a b
Right a
x)
Maybe a
_ -> Either [Doc] a -> ST s (Either [Doc] a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ([Doc] -> Either [Doc] a
forall a b. a -> Either a b
Left ((ErrMsg -> Doc) -> [ErrMsg] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ErrMsg -> Doc
renderErrMsg ([ErrMsg] -> [ErrMsg]
forall a. [a] -> [a]
reverse [ErrMsg]
final_errs)))
type ErrCtx s = MsgDoc
data UnifEnv s = UnifEnv {
forall s. UnifEnv s -> UnifRef s Int
unify_uniq :: UnifRef s UnitIdUnique,
forall s. UnifEnv s -> UnifRef s (Map ModuleName (ModuleU s))
unify_reqs :: UnifRef s (Map ModuleName (ModuleU s)),
forall s. UnifEnv s -> ComponentId
unify_self_cid :: ComponentId,
forall s. UnifEnv s -> Verbosity
unify_verbosity :: Verbosity,
forall s. UnifEnv s -> [Doc]
unify_ctx :: [ErrCtx s],
forall s. UnifEnv s -> FullDb
unify_db :: FullDb,
forall s. UnifEnv s -> UnifRef s [ErrMsg]
unify_errs :: UnifRef s [ErrMsg]
}
instance Functor (UnifyM s) where
fmap :: forall a b. (a -> b) -> UnifyM s a -> UnifyM s b
fmap a -> b
f (UnifyM UnifEnv s -> ST s (Maybe a)
m) = (UnifEnv s -> ST s (Maybe b)) -> UnifyM s b
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((ST s (Maybe a) -> ST s (Maybe b))
-> (UnifEnv s -> ST s (Maybe a)) -> UnifEnv s -> ST s (Maybe b)
forall a b. (a -> b) -> (UnifEnv s -> a) -> UnifEnv s -> b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Maybe a -> Maybe b) -> ST s (Maybe a) -> ST s (Maybe b)
forall a b. (a -> b) -> ST s a -> ST s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((a -> b) -> Maybe a -> Maybe b
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f)) UnifEnv s -> ST s (Maybe a)
m)
instance Applicative (UnifyM s) where
pure :: forall a. a -> UnifyM s a
pure = (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe a)) -> UnifyM s a)
-> (a -> UnifEnv s -> ST s (Maybe a)) -> a -> UnifyM s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ST s (Maybe a) -> UnifEnv s -> ST s (Maybe a)
forall a. a -> UnifEnv s -> a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ST s (Maybe a) -> UnifEnv s -> ST s (Maybe a))
-> (a -> ST s (Maybe a)) -> a -> UnifEnv s -> ST s (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Maybe a -> ST s (Maybe a)
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> ST s (Maybe a))
-> (a -> Maybe a) -> a -> ST s (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
UnifyM UnifEnv s -> ST s (Maybe (a -> b))
f <*> :: forall a b. UnifyM s (a -> b) -> UnifyM s a -> UnifyM s b
<*> UnifyM UnifEnv s -> ST s (Maybe a)
x = (UnifEnv s -> ST s (Maybe b)) -> UnifyM s b
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe b)) -> UnifyM s b)
-> (UnifEnv s -> ST s (Maybe b)) -> UnifyM s b
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
r -> do
Maybe (a -> b)
f' <- UnifEnv s -> ST s (Maybe (a -> b))
f UnifEnv s
r
case Maybe (a -> b)
f' of
Maybe (a -> b)
Nothing -> Maybe b -> ST s (Maybe b)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a -> b
f'' -> do
Maybe a
x' <- UnifEnv s -> ST s (Maybe a)
x UnifEnv s
r
case Maybe a
x' of
Maybe a
Nothing -> Maybe b -> ST s (Maybe b)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a
x'' -> Maybe b -> ST s (Maybe b)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (b -> Maybe b
forall a. a -> Maybe a
Just (a -> b
f'' a
x''))
instance Monad (UnifyM s) where
return :: forall a. a -> UnifyM s a
return = a -> UnifyM s a
forall a. a -> UnifyM s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure
UnifyM UnifEnv s -> ST s (Maybe a)
m >>= :: forall a b. UnifyM s a -> (a -> UnifyM s b) -> UnifyM s b
>>= a -> UnifyM s b
f = (UnifEnv s -> ST s (Maybe b)) -> UnifyM s b
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe b)) -> UnifyM s b)
-> (UnifEnv s -> ST s (Maybe b)) -> UnifyM s b
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
r -> do
Maybe a
x <- UnifEnv s -> ST s (Maybe a)
m UnifEnv s
r
case Maybe a
x of
Maybe a
Nothing -> Maybe b -> ST s (Maybe b)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe b
forall a. Maybe a
Nothing
Just a
x' -> UnifyM s b -> UnifEnv s -> ST s (Maybe b)
forall s a. UnifyM s a -> UnifEnv s -> ST s (Maybe a)
unUnifyM (a -> UnifyM s b
f a
x') UnifEnv s
r
liftST :: ST s a -> UnifyM s a
liftST :: forall s a. ST s a -> UnifyM s a
liftST ST s a
m = (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe a)) -> UnifyM s a)
-> (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
_ -> (a -> Maybe a) -> ST s a -> ST s (Maybe a)
forall a b. (a -> b) -> ST s a -> ST s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Maybe a
forall a. a -> Maybe a
Just ST s a
m
addErr :: MsgDoc -> UnifyM s ()
addErr :: forall s. Doc -> UnifyM s ()
addErr Doc
msg = do
UnifEnv s
env <- UnifyM s (UnifEnv s)
forall s. UnifyM s (UnifEnv s)
getUnifEnv
let err :: ErrMsg
err = ErrMsg {
err_msg :: Doc
err_msg = Doc
msg,
err_ctx :: [Doc]
err_ctx = UnifEnv s -> [Doc]
forall s. UnifEnv s -> [Doc]
unify_ctx UnifEnv s
env
}
ST s () -> UnifyM s ()
forall s a. ST s a -> UnifyM s a
liftST (ST s () -> UnifyM s ()) -> ST s () -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$ STRef s [ErrMsg] -> ([ErrMsg] -> [ErrMsg]) -> ST s ()
forall s a. STRef s a -> (a -> a) -> ST s ()
modifySTRef (UnifEnv s -> STRef s [ErrMsg]
forall s. UnifEnv s -> UnifRef s [ErrMsg]
unify_errs UnifEnv s
env) (\[ErrMsg]
errs -> ErrMsg
errErrMsg -> [ErrMsg] -> [ErrMsg]
forall a. a -> [a] -> [a]
:[ErrMsg]
errs)
failWith :: MsgDoc -> UnifyM s a
failWith :: forall s a. Doc -> UnifyM s a
failWith Doc
msg = do
Doc -> UnifyM s ()
forall s. Doc -> UnifyM s ()
addErr Doc
msg
UnifyM s a
forall s a. UnifyM s a
failM
failM :: UnifyM s a
failM :: forall s a. UnifyM s a
failM = (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe a)) -> UnifyM s a)
-> (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
_ -> Maybe a -> ST s (Maybe a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe a
forall a. Maybe a
Nothing
failIfErrs :: UnifyM s ()
failIfErrs :: forall s. UnifyM s ()
failIfErrs = do
UnifEnv s
env <- UnifyM s (UnifEnv s)
forall s. UnifyM s (UnifEnv s)
getUnifEnv
[ErrMsg]
errs <- ST s [ErrMsg] -> UnifyM s [ErrMsg]
forall s a. ST s a -> UnifyM s a
liftST (ST s [ErrMsg] -> UnifyM s [ErrMsg])
-> ST s [ErrMsg] -> UnifyM s [ErrMsg]
forall a b. (a -> b) -> a -> b
$ STRef s [ErrMsg] -> ST s [ErrMsg]
forall s a. STRef s a -> ST s a
readSTRef (UnifEnv s -> STRef s [ErrMsg]
forall s. UnifEnv s -> UnifRef s [ErrMsg]
unify_errs UnifEnv s
env)
Bool -> UnifyM s () -> UnifyM s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Bool -> Bool
not ([ErrMsg] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [ErrMsg]
errs)) UnifyM s ()
forall s a. UnifyM s a
failM
tryM :: UnifyM s a -> UnifyM s (Maybe a)
tryM :: forall s a. UnifyM s a -> UnifyM s (Maybe a)
tryM UnifyM s a
m =
(UnifEnv s -> ST s (Maybe (Maybe a))) -> UnifyM s (Maybe a)
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM (\UnifEnv s
env -> do
Maybe a
mb_r <- UnifyM s a -> UnifEnv s -> ST s (Maybe a)
forall s a. UnifyM s a -> UnifEnv s -> ST s (Maybe a)
unUnifyM UnifyM s a
m UnifEnv s
env
Maybe (Maybe a) -> ST s (Maybe (Maybe a))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe a -> Maybe (Maybe a)
forall a. a -> Maybe a
Just Maybe a
mb_r))
type UnifRef s a = STRef s a
readUnifRef :: UnifRef s a -> UnifyM s a
readUnifRef :: forall s a. UnifRef s a -> UnifyM s a
readUnifRef = ST s a -> UnifyM s a
forall s a. ST s a -> UnifyM s a
liftST (ST s a -> UnifyM s a)
-> (UnifRef s a -> ST s a) -> UnifRef s a -> UnifyM s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnifRef s a -> ST s a
forall s a. STRef s a -> ST s a
readSTRef
writeUnifRef :: UnifRef s a -> a -> UnifyM s ()
writeUnifRef :: forall s a. UnifRef s a -> a -> UnifyM s ()
writeUnifRef UnifRef s a
x = ST s () -> UnifyM s ()
forall s a. ST s a -> UnifyM s a
liftST (ST s () -> UnifyM s ()) -> (a -> ST s ()) -> a -> UnifyM s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. UnifRef s a -> a -> ST s ()
forall s a. STRef s a -> a -> ST s ()
writeSTRef UnifRef s a
x
getUnifEnv :: UnifyM s (UnifEnv s)
getUnifEnv :: forall s. UnifyM s (UnifEnv s)
getUnifEnv = (UnifEnv s -> ST s (Maybe (UnifEnv s))) -> UnifyM s (UnifEnv s)
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe (UnifEnv s))) -> UnifyM s (UnifEnv s))
-> (UnifEnv s -> ST s (Maybe (UnifEnv s))) -> UnifyM s (UnifEnv s)
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
r -> Maybe (UnifEnv s) -> ST s (Maybe (UnifEnv s))
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (UnifEnv s -> Maybe (UnifEnv s)
forall a. a -> Maybe a
forall (m :: * -> *) a. Monad m => a -> m a
return UnifEnv s
r)
addErrContext :: Doc -> UnifyM s a -> UnifyM s a
addErrContext :: forall s a. Doc -> UnifyM s a -> UnifyM s a
addErrContext Doc
ctx UnifyM s a
m = Doc -> UnifyM s a -> UnifyM s a
forall s a. Doc -> UnifyM s a -> UnifyM s a
addErrContextM Doc
ctx UnifyM s a
m
addErrContextM :: ErrCtx s -> UnifyM s a -> UnifyM s a
addErrContextM :: forall s a. Doc -> UnifyM s a -> UnifyM s a
addErrContextM Doc
ctx UnifyM s a
m =
(UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall s a. (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
UnifyM ((UnifEnv s -> ST s (Maybe a)) -> UnifyM s a)
-> (UnifEnv s -> ST s (Maybe a)) -> UnifyM s a
forall a b. (a -> b) -> a -> b
$ \UnifEnv s
r -> UnifyM s a -> UnifEnv s -> ST s (Maybe a)
forall s a. UnifyM s a -> UnifEnv s -> ST s (Maybe a)
unUnifyM UnifyM s a
m UnifEnv s
r { unify_ctx = ctx : unify_ctx r }
data ModuleU' s
= ModuleU (UnitIdU s) ModuleName
| ModuleVarU ModuleName
data UnitIdU' s
= UnitIdU UnitIdUnique ComponentId (Map ModuleName (ModuleU s))
| UnitIdThunkU DefUnitId
type ModuleU s = UnionFind.Point s (ModuleU' s)
type UnitIdU s = UnionFind.Point s (UnitIdU' s)
type UnitIdUnique = Int
type MuEnv s = (IntMap (UnitIdU s), Int)
extendMuEnv :: MuEnv s -> UnitIdU s -> MuEnv s
extendMuEnv :: forall s. MuEnv s -> UnitIdU s -> MuEnv s
extendMuEnv (IntMap (UnitIdU s)
m, Int
i) UnitIdU s
x =
(Int -> UnitIdU s -> IntMap (UnitIdU s) -> IntMap (UnitIdU s)
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) UnitIdU s
x IntMap (UnitIdU s)
m, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
emptyMuEnv :: MuEnv s
emptyMuEnv :: forall s. MuEnv s
emptyMuEnv = (IntMap (UnitIdU s)
forall a. IntMap a
IntMap.empty, -Int
1)
convertUnitId' :: MuEnv s
-> OpenUnitId
-> UnifyM s (UnitIdU s)
convertUnitId' :: forall s. MuEnv s -> OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId' MuEnv s
_ (DefiniteUnitId DefUnitId
uid) =
ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (UnitIdU s) -> UnifyM s (UnitIdU s))
-> ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall a b. (a -> b) -> a -> b
$ UnitIdU' s -> ST s (UnitIdU s)
forall a s. a -> ST s (Point s a)
UnionFind.fresh (DefUnitId -> UnitIdU' s
forall s. DefUnitId -> UnitIdU' s
UnitIdThunkU DefUnitId
uid)
convertUnitId' MuEnv s
stk (IndefFullUnitId ComponentId
cid OpenModuleSubst
insts) = do
UnifRef s Int
fs <- (UnifEnv s -> UnifRef s Int)
-> UnifyM s (UnifEnv s) -> UnifyM s (UnifRef s Int)
forall a b. (a -> b) -> UnifyM s a -> UnifyM s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UnifEnv s -> UnifRef s Int
forall s. UnifEnv s -> UnifRef s Int
unify_uniq UnifyM s (UnifEnv s)
forall s. UnifyM s (UnifEnv s)
getUnifEnv
UnitIdU s
x <- ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (UnitIdU s) -> UnifyM s (UnitIdU s))
-> ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall a b. (a -> b) -> a -> b
$ UnitIdU' s -> ST s (UnitIdU s)
forall a s. a -> ST s (Point s a)
UnionFind.fresh ([Char] -> UnitIdU' s
forall a. HasCallStack => [Char] -> a
error [Char]
"convertUnitId")
Map ModuleName (ModuleU s)
insts_u <- OpenModuleSubst
-> (OpenModule -> UnifyM s (ModuleU s))
-> UnifyM s (Map ModuleName (ModuleU s))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for OpenModuleSubst
insts ((OpenModule -> UnifyM s (ModuleU s))
-> UnifyM s (Map ModuleName (ModuleU s)))
-> (OpenModule -> UnifyM s (ModuleU s))
-> UnifyM s (Map ModuleName (ModuleU s))
forall a b. (a -> b) -> a -> b
$ MuEnv s -> OpenModule -> UnifyM s (ModuleU s)
forall s. MuEnv s -> OpenModule -> UnifyM s (ModuleU s)
convertModule' (MuEnv s -> UnitIdU s -> MuEnv s
forall s. MuEnv s -> UnitIdU s -> MuEnv s
extendMuEnv MuEnv s
stk UnitIdU s
x)
Int
u <- UnifRef s Int -> UnifyM s Int
forall s a. UnifRef s a -> UnifyM s a
readUnifRef UnifRef s Int
fs
UnifRef s Int -> Int -> UnifyM s ()
forall s a. UnifRef s a -> a -> UnifyM s ()
writeUnifRef UnifRef s Int
fs (Int
uInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
1)
UnitIdU s
y <- ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (UnitIdU s) -> UnifyM s (UnitIdU s))
-> ST s (UnitIdU s) -> UnifyM s (UnitIdU s)
forall a b. (a -> b) -> a -> b
$ UnitIdU' s -> ST s (UnitIdU s)
forall a s. a -> ST s (Point s a)
UnionFind.fresh (Int -> ComponentId -> Map ModuleName (ModuleU s) -> UnitIdU' s
forall s.
Int -> ComponentId -> Map ModuleName (ModuleU s) -> UnitIdU' s
UnitIdU Int
u ComponentId
cid Map ModuleName (ModuleU s)
insts_u)
ST s () -> UnifyM s ()
forall s a. ST s a -> UnifyM s a
liftST (ST s () -> UnifyM s ()) -> ST s () -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$ UnitIdU s -> UnitIdU s -> ST s ()
forall s a. Point s a -> Point s a -> ST s ()
UnionFind.union UnitIdU s
x UnitIdU s
y
UnitIdU s -> UnifyM s (UnitIdU s)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return UnitIdU s
y
convertModule' :: MuEnv s
-> OpenModule -> UnifyM s (ModuleU s)
convertModule' :: forall s. MuEnv s -> OpenModule -> UnifyM s (ModuleU s)
convertModule' MuEnv s
_stk (OpenModuleVar ModuleName
mod_name) = do
UnifRef s (Map ModuleName (ModuleU s))
hmap <- (UnifEnv s -> UnifRef s (Map ModuleName (ModuleU s)))
-> UnifyM s (UnifEnv s)
-> UnifyM s (UnifRef s (Map ModuleName (ModuleU s)))
forall a b. (a -> b) -> UnifyM s a -> UnifyM s b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap UnifEnv s -> UnifRef s (Map ModuleName (ModuleU s))
forall s. UnifEnv s -> UnifRef s (Map ModuleName (ModuleU s))
unify_reqs UnifyM s (UnifEnv s)
forall s. UnifyM s (UnifEnv s)
getUnifEnv
Map ModuleName (ModuleU s)
hm <- UnifRef s (Map ModuleName (ModuleU s))
-> UnifyM s (Map ModuleName (ModuleU s))
forall s a. UnifRef s a -> UnifyM s a
readUnifRef UnifRef s (Map ModuleName (ModuleU s))
hmap
case ModuleName -> Map ModuleName (ModuleU s) -> Maybe (ModuleU s)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ModuleName
mod_name Map ModuleName (ModuleU s)
hm of
Maybe (ModuleU s)
Nothing -> do ModuleU s
mod <- ST s (ModuleU s) -> UnifyM s (ModuleU s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (ModuleU s) -> UnifyM s (ModuleU s))
-> ST s (ModuleU s) -> UnifyM s (ModuleU s)
forall a b. (a -> b) -> a -> b
$ ModuleU' s -> ST s (ModuleU s)
forall a s. a -> ST s (Point s a)
UnionFind.fresh (ModuleName -> ModuleU' s
forall s. ModuleName -> ModuleU' s
ModuleVarU ModuleName
mod_name)
UnifRef s (Map ModuleName (ModuleU s))
-> Map ModuleName (ModuleU s) -> UnifyM s ()
forall s a. UnifRef s a -> a -> UnifyM s ()
writeUnifRef UnifRef s (Map ModuleName (ModuleU s))
hmap (ModuleName
-> ModuleU s
-> Map ModuleName (ModuleU s)
-> Map ModuleName (ModuleU s)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ModuleName
mod_name ModuleU s
mod Map ModuleName (ModuleU s)
hm)
ModuleU s -> UnifyM s (ModuleU s)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ModuleU s
mod
Just ModuleU s
mod -> ModuleU s -> UnifyM s (ModuleU s)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ModuleU s
mod
convertModule' MuEnv s
stk (OpenModule OpenUnitId
uid ModuleName
mod_name) = do
UnitIdU s
uid_u <- MuEnv s -> OpenUnitId -> UnifyM s (UnitIdU s)
forall s. MuEnv s -> OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId' MuEnv s
stk OpenUnitId
uid
ST s (ModuleU s) -> UnifyM s (ModuleU s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (ModuleU s) -> UnifyM s (ModuleU s))
-> ST s (ModuleU s) -> UnifyM s (ModuleU s)
forall a b. (a -> b) -> a -> b
$ ModuleU' s -> ST s (ModuleU s)
forall a s. a -> ST s (Point s a)
UnionFind.fresh (UnitIdU s -> ModuleName -> ModuleU' s
forall s. UnitIdU s -> ModuleName -> ModuleU' s
ModuleU UnitIdU s
uid_u ModuleName
mod_name)
convertUnitId :: OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId :: forall s. OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId = MuEnv s -> OpenUnitId -> UnifyM s (UnitIdU s)
forall s. MuEnv s -> OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId' MuEnv s
forall s. MuEnv s
emptyMuEnv
convertModule :: OpenModule -> UnifyM s (ModuleU s)
convertModule :: forall s. OpenModule -> UnifyM s (ModuleU s)
convertModule = MuEnv s -> OpenModule -> UnifyM s (ModuleU s)
forall s. MuEnv s -> OpenModule -> UnifyM s (ModuleU s)
convertModule' MuEnv s
forall s. MuEnv s
emptyMuEnv
type ModuleSubstU s = Map ModuleName (ModuleU s)
convertModuleSubst :: Map ModuleName OpenModule -> UnifyM s (Map ModuleName (ModuleU s))
convertModuleSubst :: forall s. OpenModuleSubst -> UnifyM s (Map ModuleName (ModuleU s))
convertModuleSubst = (OpenModule -> UnifyM s (ModuleU s))
-> OpenModuleSubst -> UnifyM s (Map ModuleName (ModuleU s))
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map ModuleName a -> f (Map ModuleName b)
traverse OpenModule -> UnifyM s (ModuleU s)
forall s. OpenModule -> UnifyM s (ModuleU s)
convertModule
convertModuleSubstU :: ModuleSubstU s -> UnifyM s OpenModuleSubst
convertModuleSubstU :: forall s. ModuleSubstU s -> UnifyM s OpenModuleSubst
convertModuleSubstU = (ModuleU s -> UnifyM s OpenModule)
-> Map ModuleName (ModuleU s) -> UnifyM s OpenModuleSubst
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map ModuleName a -> f (Map ModuleName b)
traverse ModuleU s -> UnifyM s OpenModule
forall s. ModuleU s -> UnifyM s OpenModule
convertModuleU
type MooEnv = (IntMap Int, Int)
emptyMooEnv :: MooEnv
emptyMooEnv :: MooEnv
emptyMooEnv = (IntMap Int
forall a. IntMap a
IntMap.empty, -Int
1)
extendMooEnv :: MooEnv -> UnitIdUnique -> MooEnv
extendMooEnv :: MooEnv -> Int -> MooEnv
extendMooEnv (IntMap Int
m, Int
i) Int
k = (Int -> Int -> IntMap Int -> IntMap Int
forall a. Int -> a -> IntMap a -> IntMap a
IntMap.insert Int
k (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) IntMap Int
m, Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
lookupMooEnv :: MooEnv -> UnitIdUnique -> Maybe Int
lookupMooEnv :: MooEnv -> Int -> Maybe Int
lookupMooEnv (IntMap Int
m, Int
i) Int
k =
case Int -> IntMap Int -> Maybe Int
forall a. Int -> IntMap a -> Maybe a
IntMap.lookup Int
k IntMap Int
m of
Maybe Int
Nothing -> Maybe Int
forall a. Maybe a
Nothing
Just Int
v -> Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
v)
convertUnitIdU' :: MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU' :: forall s. MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU' MooEnv
stk UnitIdU s
uid_u Doc
required_mod_name = do
UnitIdU' s
x <- ST s (UnitIdU' s) -> UnifyM s (UnitIdU' s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (UnitIdU' s) -> UnifyM s (UnitIdU' s))
-> ST s (UnitIdU' s) -> UnifyM s (UnitIdU' s)
forall a b. (a -> b) -> a -> b
$ UnitIdU s -> ST s (UnitIdU' s)
forall s a. Point s a -> ST s a
UnionFind.find UnitIdU s
uid_u
case UnitIdU' s
x of
UnitIdThunkU DefUnitId
uid -> OpenUnitId -> UnifyM s OpenUnitId
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (OpenUnitId -> UnifyM s OpenUnitId)
-> OpenUnitId -> UnifyM s OpenUnitId
forall a b. (a -> b) -> a -> b
$ DefUnitId -> OpenUnitId
DefiniteUnitId DefUnitId
uid
UnitIdU Int
u ComponentId
cid Map ModuleName (ModuleU s)
insts_u ->
case MooEnv -> Int -> Maybe Int
lookupMooEnv MooEnv
stk Int
u of
Just Int
_ ->
let mod_names :: [ModuleName]
mod_names = Map ModuleName (ModuleU s) -> [ModuleName]
forall k a. Map k a -> [k]
Map.keys Map ModuleName (ModuleU s)
insts_u
in Doc -> [ModuleName] -> UnifyM s OpenUnitId
forall s a. Doc -> [ModuleName] -> UnifyM s a
failWithMutuallyRecursiveUnitsError Doc
required_mod_name [ModuleName]
mod_names
Maybe Int
Nothing -> do
OpenModuleSubst
insts <- Map ModuleName (ModuleU s)
-> (ModuleU s -> UnifyM s OpenModule) -> UnifyM s OpenModuleSubst
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for Map ModuleName (ModuleU s)
insts_u ((ModuleU s -> UnifyM s OpenModule) -> UnifyM s OpenModuleSubst)
-> (ModuleU s -> UnifyM s OpenModule) -> UnifyM s OpenModuleSubst
forall a b. (a -> b) -> a -> b
$ MooEnv -> ModuleU s -> UnifyM s OpenModule
forall s. MooEnv -> ModuleU s -> UnifyM s OpenModule
convertModuleU' (MooEnv -> Int -> MooEnv
extendMooEnv MooEnv
stk Int
u)
OpenUnitId -> UnifyM s OpenUnitId
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (OpenUnitId -> UnifyM s OpenUnitId)
-> OpenUnitId -> UnifyM s OpenUnitId
forall a b. (a -> b) -> a -> b
$ ComponentId -> OpenModuleSubst -> OpenUnitId
IndefFullUnitId ComponentId
cid OpenModuleSubst
insts
convertModuleU' :: MooEnv -> ModuleU s -> UnifyM s OpenModule
convertModuleU' :: forall s. MooEnv -> ModuleU s -> UnifyM s OpenModule
convertModuleU' MooEnv
stk ModuleU s
mod_u = do
ModuleU' s
mod <- ST s (ModuleU' s) -> UnifyM s (ModuleU' s)
forall s a. ST s a -> UnifyM s a
liftST (ST s (ModuleU' s) -> UnifyM s (ModuleU' s))
-> ST s (ModuleU' s) -> UnifyM s (ModuleU' s)
forall a b. (a -> b) -> a -> b
$ ModuleU s -> ST s (ModuleU' s)
forall s a. Point s a -> ST s a
UnionFind.find ModuleU s
mod_u
case ModuleU' s
mod of
ModuleVarU ModuleName
mod_name -> OpenModule -> UnifyM s OpenModule
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (ModuleName -> OpenModule
OpenModuleVar ModuleName
mod_name)
ModuleU UnitIdU s
uid_u ModuleName
mod_name -> do
OpenUnitId
uid <- MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
forall s. MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU' MooEnv
stk UnitIdU s
uid_u (ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty ModuleName
mod_name)
OpenModule -> UnifyM s OpenModule
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (OpenUnitId -> ModuleName -> OpenModule
OpenModule OpenUnitId
uid ModuleName
mod_name)
failWithMutuallyRecursiveUnitsError :: Doc -> [ModuleName] -> UnifyM s a
failWithMutuallyRecursiveUnitsError :: forall s a. Doc -> [ModuleName] -> UnifyM s a
failWithMutuallyRecursiveUnitsError Doc
required_mod_name [ModuleName]
mod_names =
let sigsList :: Doc
sigsList = [Doc] -> Doc
hcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
punctuate ([Char] -> Doc
text [Char]
", ") ([Doc] -> [Doc]) -> [Doc] -> [Doc]
forall a b. (a -> b) -> a -> b
$ (ModuleName -> Doc) -> [ModuleName] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map (Doc -> Doc
quotes (Doc -> Doc) -> (ModuleName -> Doc) -> ModuleName -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty) [ModuleName]
mod_names in
Doc -> UnifyM s a
forall s a. Doc -> UnifyM s a
failWith (Doc -> UnifyM s a) -> Doc -> UnifyM s a
forall a b. (a -> b) -> a -> b
$
[Char] -> Doc
text [Char]
"Cannot instantiate requirement" Doc -> Doc -> Doc
<+> Doc -> Doc
quotes Doc
required_mod_name Doc -> Doc -> Doc
$$
[Char] -> Doc
text [Char]
"Ensure \"build-depends:\" doesn't include any library with signatures:" Doc -> Doc -> Doc
<+> Doc
sigsList Doc -> Doc -> Doc
$$
[Char] -> Doc
text [Char]
"as this creates a cyclic dependency, which GHC does not support."
convertUnitIdU :: UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU :: forall s. UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU = MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
forall s. MooEnv -> UnitIdU s -> Doc -> UnifyM s OpenUnitId
convertUnitIdU' MooEnv
emptyMooEnv
convertModuleU :: ModuleU s -> UnifyM s OpenModule
convertModuleU :: forall s. ModuleU s -> UnifyM s OpenModule
convertModuleU = MooEnv -> ModuleU s -> UnifyM s OpenModule
forall s. MooEnv -> ModuleU s -> UnifyM s OpenModule
convertModuleU' MooEnv
emptyMooEnv
emptyModuleScopeU :: ModuleScopeU s
emptyModuleScopeU :: forall s. ModuleScopeU s
emptyModuleScopeU = (Map ModuleName [ModuleWithSourceU s]
forall k a. Map k a
Map.empty, Map ModuleName [ModuleWithSourceU s]
forall k a. Map k a
Map.empty)
type ModuleScopeU s = (ModuleProvidesU s, ModuleRequiresU s)
type ModuleProvidesU s = Map ModuleName [ModuleWithSourceU s]
type ModuleRequiresU s = ModuleProvidesU s
type ModuleWithSourceU s = WithSource (ModuleU s)
ci_msg :: ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming -> Doc
ci_msg :: ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming -> Doc
ci_msg ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci
| ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming -> Bool
forall id rn. ComponentInclude id rn -> Bool
ci_implicit ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci = [Char] -> Doc
text [Char]
"build-depends:" Doc -> Doc -> Doc
<+> Doc
pp_pn
| Bool
otherwise = [Char] -> Doc
text [Char]
"mixins:" Doc -> Doc -> Doc
<+> Doc
pp_pn Doc -> Doc -> Doc
<+> IncludeRenaming -> Doc
forall a. Pretty a => a -> Doc
pretty (ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
-> IncludeRenaming
forall id rn. ComponentInclude id rn -> rn
ci_renaming ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci)
where
pn :: PackageName
pn = PackageIdentifier -> PackageName
pkgName (ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
-> PackageIdentifier
forall id rn. ComponentInclude id rn -> PackageIdentifier
ci_pkgid ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci)
pp_pn :: Doc
pp_pn =
case ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
-> ComponentName
forall id rn. ComponentInclude id rn -> ComponentName
ci_cname ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci of
CLibName LibraryName
LMainLibName -> PackageName -> Doc
forall a. Pretty a => a -> Doc
pretty PackageName
pn
CLibName (LSubLibName UnqualComponentName
cn) -> PackageName -> Doc
forall a. Pretty a => a -> Doc
pretty PackageName
pn Doc -> Doc -> Doc
<<>> Doc
colon Doc -> Doc -> Doc
<<>> UnqualComponentName -> Doc
forall a. Pretty a => a -> Doc
pretty UnqualComponentName
cn
ComponentName
cn -> PackageName -> Doc
forall a. Pretty a => a -> Doc
pretty PackageName
pn Doc -> Doc -> Doc
<+> Doc -> Doc
parens (ComponentName -> Doc
forall a. Pretty a => a -> Doc
pretty ComponentName
cn)
convertInclude
:: ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
-> UnifyM s (ModuleScopeU s,
Either (ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming) )
convertInclude :: forall s.
ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
convertInclude ci :: ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci@(ComponentInclude {
ci_ann_id :: forall id rn. ComponentInclude id rn -> AnnotatedId id
ci_ann_id = AnnotatedId {
ann_id :: forall id. AnnotatedId id -> id
ann_id = (OpenUnitId
uid, ModuleShape OpenModuleSubst
provs Set ModuleName
reqs),
ann_pid :: forall id. AnnotatedId id -> PackageIdentifier
ann_pid = PackageIdentifier
pid,
ann_cname :: forall id. AnnotatedId id -> ComponentName
ann_cname = ComponentName
compname
},
ci_renaming :: forall id rn. ComponentInclude id rn -> rn
ci_renaming = incl :: IncludeRenaming
incl@(IncludeRenaming ModuleRenaming
prov_rns ModuleRenaming
req_rns),
ci_implicit :: forall id rn. ComponentInclude id rn -> Bool
ci_implicit = Bool
implicit
}) = Doc
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
forall s a. Doc -> UnifyM s a -> UnifyM s a
addErrContext ([Char] -> Doc
text [Char]
"In" Doc -> Doc -> Doc
<+> ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming -> Doc
ci_msg ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci) (UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming)))
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
forall a b. (a -> b) -> a -> b
$ do
let pn :: PackageName
pn = PackageIdentifier -> PackageName
forall pkg. Package pkg => pkg -> PackageName
packageName PackageIdentifier
pid
the_source :: ModuleSource
the_source | Bool
implicit
= PackageName -> ComponentName -> ModuleSource
FromBuildDepends PackageName
pn ComponentName
compname
| Bool
otherwise
= PackageName -> ComponentName -> IncludeRenaming -> ModuleSource
FromMixins PackageName
pn ComponentName
compname IncludeRenaming
incl
source :: a -> WithSource a
source = ModuleSource -> a -> WithSource a
forall a. ModuleSource -> a -> WithSource a
WithSource ModuleSource
the_source
[(ModuleName, ModuleName)]
req_rename_list <-
case ModuleRenaming
req_rns of
ModuleRenaming
DefaultRenaming -> [(ModuleName, ModuleName)] -> UnifyM s [(ModuleName, ModuleName)]
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return []
HidingRenaming [ModuleName]
_ -> do
Doc -> UnifyM s ()
forall s. Doc -> UnifyM s ()
addErr (Doc -> UnifyM s ()) -> Doc -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$ [Char] -> Doc
text [Char]
"Unsupported syntax" Doc -> Doc -> Doc
<+>
Doc -> Doc
quotes ([Char] -> Doc
text [Char]
"requires hiding (...)")
[(ModuleName, ModuleName)] -> UnifyM s [(ModuleName, ModuleName)]
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return []
ModuleRenaming [(ModuleName, ModuleName)]
rns -> [(ModuleName, ModuleName)] -> UnifyM s [(ModuleName, ModuleName)]
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return [(ModuleName, ModuleName)]
rns
let req_rename_listmap :: Map ModuleName [ModuleName]
req_rename_listmap :: Map ModuleName [ModuleName]
req_rename_listmap =
([ModuleName] -> [ModuleName] -> [ModuleName])
-> [(ModuleName, [ModuleName])] -> Map ModuleName [ModuleName]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [ModuleName] -> [ModuleName] -> [ModuleName]
forall a. [a] -> [a] -> [a]
(++) [ (ModuleName
k,[ModuleName
v]) | (ModuleName
k,ModuleName
v) <- [(ModuleName, ModuleName)]
req_rename_list ]
Map ModuleName ModuleName
req_rename <- Map ModuleName (UnifyM s ModuleName)
-> UnifyM s (Map ModuleName ModuleName)
forall (t :: * -> *) (f :: * -> *) a.
(Traversable t, Applicative f) =>
t (f a) -> f (t a)
forall (f :: * -> *) a.
Applicative f =>
Map ModuleName (f a) -> f (Map ModuleName a)
sequenceA (Map ModuleName (UnifyM s ModuleName)
-> UnifyM s (Map ModuleName ModuleName))
-> ((ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> Map ModuleName (UnifyM s ModuleName))
-> (ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> UnifyM s (Map ModuleName ModuleName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> Map ModuleName [ModuleName]
-> Map ModuleName (UnifyM s ModuleName))
-> Map ModuleName [ModuleName]
-> (ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> Map ModuleName (UnifyM s ModuleName)
forall a b c. (a -> b -> c) -> b -> a -> c
flip (ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> Map ModuleName [ModuleName]
-> Map ModuleName (UnifyM s ModuleName)
forall k a b. (k -> a -> b) -> Map k a -> Map k b
Map.mapWithKey Map ModuleName [ModuleName]
req_rename_listmap ((ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> UnifyM s (Map ModuleName ModuleName))
-> (ModuleName -> [ModuleName] -> UnifyM s ModuleName)
-> UnifyM s (Map ModuleName ModuleName)
forall a b. (a -> b) -> a -> b
$ \ModuleName
k [ModuleName]
vs0 ->
case [ModuleName]
vs0 of
[] -> [Char] -> UnifyM s ModuleName
forall a. HasCallStack => [Char] -> a
error [Char]
"req_rename"
[ModuleName
v] -> ModuleName -> UnifyM s ModuleName
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ModuleName
v
ModuleName
v:[ModuleName]
vs -> do Doc -> UnifyM s ()
forall s. Doc -> UnifyM s ()
addErr (Doc -> UnifyM s ()) -> Doc -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$
[Char] -> Doc
text [Char]
"Conflicting renamings of requirement" Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty ModuleName
k) Doc -> Doc -> Doc
$$
[Char] -> Doc
text [Char]
"Renamed to: " Doc -> Doc -> Doc
<+> [Doc] -> Doc
vcat ((ModuleName -> Doc) -> [ModuleName] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty (ModuleName
vModuleName -> [ModuleName] -> [ModuleName]
forall a. a -> [a] -> [a]
:[ModuleName]
vs))
ModuleName -> UnifyM s ModuleName
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ModuleName
v
let req_rename_fn :: ModuleName -> ModuleName
req_rename_fn ModuleName
k = case ModuleName -> Map ModuleName ModuleName -> Maybe ModuleName
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ModuleName
k Map ModuleName ModuleName
req_rename of
Maybe ModuleName
Nothing -> ModuleName
k
Just ModuleName
v -> ModuleName
v
let req_subst :: OpenModuleSubst
req_subst = (ModuleName -> OpenModule)
-> Map ModuleName ModuleName -> OpenModuleSubst
forall a b. (a -> b) -> Map ModuleName a -> Map ModuleName b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ModuleName -> OpenModule
OpenModuleVar Map ModuleName ModuleName
req_rename
UnitIdU s
uid_u <- OpenUnitId -> UnifyM s (UnitIdU s)
forall s. OpenUnitId -> UnifyM s (UnitIdU s)
convertUnitId (OpenModuleSubst -> OpenUnitId -> OpenUnitId
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
req_subst OpenUnitId
uid)
ModuleRequiresU s
reqs_u <- ModuleRequires -> UnifyM s (ModuleRequiresU s)
forall s. ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleRequires (ModuleRequires -> UnifyM s (ModuleRequiresU s))
-> ([(ModuleName, [ModuleWithSource])] -> ModuleRequires)
-> [(ModuleName, [ModuleWithSource])]
-> UnifyM s (ModuleRequiresU s)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(ModuleName, [ModuleWithSource])] -> ModuleRequires
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList ([(ModuleName, [ModuleWithSource])]
-> UnifyM s (ModuleRequiresU s))
-> [(ModuleName, [ModuleWithSource])]
-> UnifyM s (ModuleRequiresU s)
forall a b. (a -> b) -> a -> b
$
[ (ModuleName
k, [OpenModule -> ModuleWithSource
forall {a}. a -> WithSource a
source (ModuleName -> OpenModule
OpenModuleVar ModuleName
k)])
| ModuleName
k <- (ModuleName -> ModuleName) -> [ModuleName] -> [ModuleName]
forall a b. (a -> b) -> [a] -> [b]
map ModuleName -> ModuleName
req_rename_fn (Set ModuleName -> [ModuleName]
forall a. Set a -> [a]
Set.toList Set ModuleName
reqs)
]
let leftover :: Set ModuleName
leftover = Map ModuleName ModuleName -> Set ModuleName
forall k a. Map k a -> Set k
Map.keysSet Map ModuleName ModuleName
req_rename Set ModuleName -> Set ModuleName -> Set ModuleName
forall a. Ord a => Set a -> Set a -> Set a
`Set.difference` Set ModuleName
reqs
Bool -> UnifyM s () -> UnifyM s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set ModuleName -> Bool
forall a. Set a -> Bool
Set.null Set ModuleName
leftover) (UnifyM s () -> UnifyM s ()) -> UnifyM s () -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$
Doc -> UnifyM s ()
forall s. Doc -> UnifyM s ()
addErr (Doc -> UnifyM s ()) -> Doc -> UnifyM s ()
forall a b. (a -> b) -> a -> b
$
Doc -> Int -> Doc -> Doc
hang ([Char] -> Doc
text [Char]
"The" Doc -> Doc -> Doc
<+> [Char] -> Doc
text (ComponentName -> [Char]
showComponentName ComponentName
compname) Doc -> Doc -> Doc
<+>
[Char] -> Doc
text [Char]
"from package" Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (PackageIdentifier -> Doc
forall a. Pretty a => a -> Doc
pretty PackageIdentifier
pid)
Doc -> Doc -> Doc
<+> [Char] -> Doc
text [Char]
"does not require:") Int
4
([Doc] -> Doc
vcat ((ModuleName -> Doc) -> [ModuleName] -> [Doc]
forall a b. (a -> b) -> [a] -> [b]
map ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty (Set ModuleName -> [ModuleName]
forall a. Set a -> [a]
Set.toList Set ModuleName
leftover)))
([(ModuleName, OpenModule)]
pre_prov_scope, ModuleRenaming
prov_rns') <-
case ModuleRenaming
prov_rns of
ModuleRenaming
DefaultRenaming -> ([(ModuleName, OpenModule)], ModuleRenaming)
-> UnifyM s ([(ModuleName, OpenModule)], ModuleRenaming)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (OpenModuleSubst -> [(ModuleName, OpenModule)]
forall k a. Map k a -> [(k, a)]
Map.toList OpenModuleSubst
provs, ModuleRenaming
prov_rns)
HidingRenaming [ModuleName]
hides ->
let hides_set :: Set ModuleName
hides_set = [ModuleName] -> Set ModuleName
forall a. Ord a => [a] -> Set a
Set.fromList [ModuleName]
hides
in let r :: [(ModuleName, OpenModule)]
r = [ (ModuleName
k,OpenModule
v)
| (ModuleName
k,OpenModule
v) <- OpenModuleSubst -> [(ModuleName, OpenModule)]
forall k a. Map k a -> [(k, a)]
Map.toList OpenModuleSubst
provs
, Bool -> Bool
not (ModuleName
k ModuleName -> Set ModuleName -> Bool
forall a. Ord a => a -> Set a -> Bool
`Set.member` Set ModuleName
hides_set) ]
in ([(ModuleName, OpenModule)], ModuleRenaming)
-> UnifyM s ([(ModuleName, OpenModule)], ModuleRenaming)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(ModuleName, OpenModule)]
r, [(ModuleName, ModuleName)] -> ModuleRenaming
ModuleRenaming (((ModuleName, OpenModule) -> (ModuleName, ModuleName))
-> [(ModuleName, OpenModule)] -> [(ModuleName, ModuleName)]
forall a b. (a -> b) -> [a] -> [b]
map ((\ModuleName
x -> (ModuleName
x,ModuleName
x))(ModuleName -> (ModuleName, ModuleName))
-> ((ModuleName, OpenModule) -> ModuleName)
-> (ModuleName, OpenModule)
-> (ModuleName, ModuleName)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.(ModuleName, OpenModule) -> ModuleName
forall a b. (a, b) -> a
fst) [(ModuleName, OpenModule)]
r))
ModuleRenaming [(ModuleName, ModuleName)]
rns -> do
[(ModuleName, OpenModule)]
r <- [UnifyM s (ModuleName, OpenModule)]
-> UnifyM s [(ModuleName, OpenModule)]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
forall (m :: * -> *) a. Monad m => [m a] -> m [a]
sequence
[ case ModuleName -> OpenModuleSubst -> Maybe OpenModule
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup ModuleName
from OpenModuleSubst
provs of
Just OpenModule
m -> (ModuleName, OpenModule) -> UnifyM s (ModuleName, OpenModule)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (ModuleName
to, OpenModule
m)
Maybe OpenModule
Nothing -> Doc -> UnifyM s (ModuleName, OpenModule)
forall s a. Doc -> UnifyM s a
failWith (Doc -> UnifyM s (ModuleName, OpenModule))
-> Doc -> UnifyM s (ModuleName, OpenModule)
forall a b. (a -> b) -> a -> b
$
[Char] -> Doc
text [Char]
"Package" Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (PackageIdentifier -> Doc
forall a. Pretty a => a -> Doc
pretty PackageIdentifier
pid) Doc -> Doc -> Doc
<+>
[Char] -> Doc
text [Char]
"does not expose the module" Doc -> Doc -> Doc
<+> Doc -> Doc
quotes (ModuleName -> Doc
forall a. Pretty a => a -> Doc
pretty ModuleName
from)
| (ModuleName
from, ModuleName
to) <- [(ModuleName, ModuleName)]
rns ]
([(ModuleName, OpenModule)], ModuleRenaming)
-> UnifyM s ([(ModuleName, OpenModule)], ModuleRenaming)
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ([(ModuleName, OpenModule)]
r, ModuleRenaming
prov_rns)
let prov_scope :: ModuleRequires
prov_scope = OpenModuleSubst -> ModuleRequires -> ModuleRequires
forall a. ModSubst a => OpenModuleSubst -> a -> a
modSubst OpenModuleSubst
req_subst
(ModuleRequires -> ModuleRequires)
-> ModuleRequires -> ModuleRequires
forall a b. (a -> b) -> a -> b
$ ([ModuleWithSource] -> [ModuleWithSource] -> [ModuleWithSource])
-> [(ModuleName, [ModuleWithSource])] -> ModuleRequires
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [ModuleWithSource] -> [ModuleWithSource] -> [ModuleWithSource]
forall a. [a] -> [a] -> [a]
(++)
[ (ModuleName
k, [OpenModule -> ModuleWithSource
forall {a}. a -> WithSource a
source OpenModule
v])
| (ModuleName
k, OpenModule
v) <- [(ModuleName, OpenModule)]
pre_prov_scope ]
ModuleRequiresU s
provs_u <- ModuleRequires -> UnifyM s (ModuleRequiresU s)
forall s. ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleProvides ModuleRequires
prov_scope
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
-> UnifyM
s
(ModuleScopeU s,
Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming))
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return ((ModuleRequiresU s
provs_u, ModuleRequiresU s
reqs_u),
(if OpenModuleSubst -> Bool
forall k a. Map k a -> Bool
Map.null OpenModuleSubst
provs Bool -> Bool -> Bool
&& Bool -> Bool
not (Set ModuleName -> Bool
forall a. Set a -> Bool
Set.null Set ModuleName
reqs)
then ComponentInclude (UnitIdU s) ModuleRenaming
-> Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming)
forall a b. b -> Either a b
Right
else ComponentInclude (UnitIdU s) ModuleRenaming
-> Either
(ComponentInclude (UnitIdU s) ModuleRenaming)
(ComponentInclude (UnitIdU s) ModuleRenaming)
forall a b. a -> Either a b
Left) (ComponentInclude {
ci_ann_id :: AnnotatedId (UnitIdU s)
ci_ann_id = AnnotatedId {
ann_id :: UnitIdU s
ann_id = UnitIdU s
uid_u,
ann_pid :: PackageIdentifier
ann_pid = PackageIdentifier
pid,
ann_cname :: ComponentName
ann_cname = ComponentName
compname
},
ci_renaming :: ModuleRenaming
ci_renaming = ModuleRenaming
prov_rns',
ci_implicit :: Bool
ci_implicit = ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming -> Bool
forall id rn. ComponentInclude id rn -> Bool
ci_implicit ComponentInclude (OpenUnitId, ModuleShape) IncludeRenaming
ci
}))
convertModuleScopeU :: ModuleScopeU s -> UnifyM s ModuleScope
convertModuleScopeU :: forall s. ModuleScopeU s -> UnifyM s ModuleScope
convertModuleScopeU (ModuleProvidesU s
provs_u, ModuleProvidesU s
reqs_u) = do
ModuleRequires
provs <- ModuleProvidesU s -> UnifyM s ModuleRequires
forall s. ModuleProvidesU s -> UnifyM s ModuleRequires
convertModuleProvidesU ModuleProvidesU s
provs_u
ModuleRequires
reqs <- ModuleProvidesU s -> UnifyM s ModuleRequires
forall s. ModuleProvidesU s -> UnifyM s ModuleRequires
convertModuleRequiresU ModuleProvidesU s
reqs_u
ModuleScope -> UnifyM s ModuleScope
forall a. a -> UnifyM s a
forall (m :: * -> *) a. Monad m => a -> m a
return (ModuleRequires -> ModuleRequires -> ModuleScope
ModuleScope ModuleRequires
provs ModuleRequires
reqs)
convertModuleProvides :: ModuleProvides -> UnifyM s (ModuleProvidesU s)
convertModuleProvides :: forall s. ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleProvides = ([ModuleWithSource] -> UnifyM s [ModuleWithSourceU s])
-> ModuleRequires
-> UnifyM s (Map ModuleName [ModuleWithSourceU s])
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map ModuleName a -> f (Map ModuleName b)
traverse ((ModuleWithSource -> UnifyM s (ModuleWithSourceU s))
-> [ModuleWithSource] -> UnifyM s [ModuleWithSourceU s]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((OpenModule -> UnifyM s (ModuleU s))
-> ModuleWithSource -> UnifyM s (ModuleWithSourceU s)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WithSource a -> f (WithSource b)
traverse OpenModule -> UnifyM s (ModuleU s)
forall s. OpenModule -> UnifyM s (ModuleU s)
convertModule))
convertModuleProvidesU :: ModuleProvidesU s -> UnifyM s ModuleProvides
convertModuleProvidesU :: forall s. ModuleProvidesU s -> UnifyM s ModuleRequires
convertModuleProvidesU = ([ModuleWithSourceU s] -> UnifyM s [ModuleWithSource])
-> Map ModuleName [ModuleWithSourceU s] -> UnifyM s ModuleRequires
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Map ModuleName a -> f (Map ModuleName b)
traverse ((ModuleWithSourceU s -> UnifyM s ModuleWithSource)
-> [ModuleWithSourceU s] -> UnifyM s [ModuleWithSource]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse ((ModuleU s -> UnifyM s OpenModule)
-> ModuleWithSourceU s -> UnifyM s ModuleWithSource
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> WithSource a -> f (WithSource b)
traverse ModuleU s -> UnifyM s OpenModule
forall s. ModuleU s -> UnifyM s OpenModule
convertModuleU))
convertModuleRequires :: ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleRequires :: forall s. ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleRequires = ModuleRequires -> UnifyM s (ModuleProvidesU s)
forall s. ModuleRequires -> UnifyM s (ModuleRequiresU s)
convertModuleProvides
convertModuleRequiresU :: ModuleRequiresU s -> UnifyM s ModuleRequires
convertModuleRequiresU :: forall s. ModuleProvidesU s -> UnifyM s ModuleRequires
convertModuleRequiresU = ModuleProvidesU s -> UnifyM s ModuleRequires
forall s. ModuleProvidesU s -> UnifyM s ModuleRequires
convertModuleProvidesU