GF.Infra.Modules: minor tweaks

Still keeping the modules both in a list and in a finite map. The overhead is
smaller than I initially thought.
This commit is contained in:
hallgren
2011-08-31 11:18:16 +00:00
parent 42e2c68d8e
commit 38db834a65
4 changed files with 20 additions and 23 deletions

View File

@@ -227,7 +227,7 @@ extendCompileEnvInt (_,gr,menv) k mfile sm = do
t <- ioeIO $ getModificationTime file t <- ioeIO $ getModificationTime file
return $ Map.insert mod (t,imps) menv return $ Map.insert mod (t,imps) menv
_ -> return menv _ -> return menv
return (k,mGrammar (sm:modules gr),menv2) --- reverse later return (k,prependModule gr sm,menv2) --- reverse later
extendCompileEnv e@(k,_,_) file sm = extendCompileEnvInt e k (Just file) sm extendCompileEnv e@(k,_,_) file sm = extendCompileEnvInt e k (Just file) sm

View File

@@ -128,7 +128,7 @@ tree2status o = case o of
buildStatus :: SourceGrammar -> Ident -> SourceModInfo -> Check Status buildStatus :: SourceGrammar -> Ident -> SourceModInfo -> Check Status
buildStatus gr c mo = let mo' = self2status c mo in do buildStatus gr c mo = let mo' = self2status c mo in do
let gr1 = mGrammar ((c,mo) : modules gr) let gr1 = prependModule gr (c,mo)
ops = [OSimple e | e <- allExtends gr1 c] ++ opens mo ops = [OSimple e | e <- allExtends gr1 c] ++ opens mo
mods <- checkErr $ mapM (lookupModule gr1 . openedModule) ops mods <- checkErr $ mapM (lookupModule gr1 . openedModule) ops
let sts = map modInfo2status $ zip ops mods let sts = map modInfo2status $ zip ops mods

View File

@@ -56,7 +56,7 @@ import qualified Data.ByteString.Char8 as BS
-- | grammar as presented to the compiler -- | grammar as presented to the compiler
type SourceGrammar = MGrammar Info type SourceGrammar = MGrammar Info
emptySourceGrammar = mGrammar [] emptySourceGrammar = emptyMGrammar
type SourceModInfo = ModInfo Info type SourceModInfo = ModInfo Info

View File

@@ -20,16 +20,16 @@
module GF.Infra.Modules ( module GF.Infra.Modules (
MGrammar, ModInfo(..), ModuleType(..), MGrammar, ModInfo(..), ModuleType(..),
MInclude (..), MInclude (..),
mGrammar,modules, mGrammar,modules,prependModule,
extends, isInherited,inheritAll, extends, isInherited,inheritAll,
updateMGrammar, updateModule, replaceJudgements, addFlag, updateModule, replaceJudgements, addFlag,
addOpenQualif, flagsModule, allFlags, addOpenQualif, flagsModule, allFlags,
OpenSpec(..), OpenSpec(..),
ModuleStatus(..), ModuleStatus(..),
openedModule, depPathModule, allDepsModule, partOfGrammar, openedModule, depPathModule, allDepsModule, partOfGrammar,
allExtends, allExtendSpecs, allExtendsPlus, allExtensions, allExtends, allExtendSpecs, allExtendsPlus, allExtensions,
searchPathModule, searchPathModule,
-- addModule, mapModules, -- addModule, mapModules, updateMGrammar,
emptyMGrammar, emptyModInfo, emptyMGrammar, emptyModInfo,
abstractOfConcrete, abstractModOfConcrete, abstractOfConcrete, abstractModOfConcrete,
lookupModule, lookupModuleType, lookupInfo, lookupModule, lookupModuleType, lookupInfo,
@@ -56,8 +56,8 @@ import Text.PrettyPrint
--mGrammar = MGrammar --mGrammar = MGrammar
--newtype MGrammar a = MGrammar {modules :: [(Ident,ModInfo a)]} --newtype MGrammar a = MGrammar {modules :: [(Ident,ModInfo a)]}
data MGrammar a = MGrammar {moduleMap :: Map.Map Ident (ModInfo a), data MGrammar a = MGrammar { moduleMap :: Map.Map Ident (ModInfo a),
modules :: [(Ident,ModInfo a)] } modules :: [(Ident,ModInfo a)] }
deriving Show deriving Show
mGrammar ms = MGrammar (Map.fromList ms) ms mGrammar ms = MGrammar (Map.fromList ms) ms
@@ -99,15 +99,15 @@ inheritAll :: Ident -> (Ident,MInclude)
inheritAll i = (i,MIAll) inheritAll i = (i,MIAll)
-- destructive update -- destructive update
{-
-- | dep order preserved since old cannot depend on new (not anymore TH 2011-08-30) -- | dep order preserved since old cannot depend on new
updateMGrammar :: MGrammar a -> MGrammar a -> MGrammar a updateMGrammar :: MGrammar a -> MGrammar a -> MGrammar a
updateMGrammar old new = mGrammar $ updateMGrammar (MGrammar omap os) (MGrammar nmap ns) =
[(i,m) | (i,m) <- os, notElem i (map fst ns)] ++ ns MGrammar (Map.union nmap omap) -- Map.union is left-biased
where ([im | im@(i,m) <- os, i `notElem` nis] ++ ns)
os = modules old where
ns = modules new nis = map fst ns
-}
updateModule :: ModInfo t -> Ident -> t -> ModInfo t updateModule :: ModInfo t -> Ident -> t -> ModInfo t
updateModule (ModInfo mt ms fs me mw ops med js) i t = ModInfo mt ms fs me mw ops med (updateTree (i,t) js) updateModule (ModInfo mt ms fs me mw ops med js) i t = ModInfo mt ms fs me mw ops med (updateTree (i,t) js)
@@ -221,6 +221,8 @@ addModule :: MGrammar a -> Ident -> ModInfo a -> MGrammar a
addModule gr name mi = MGrammar $ Map.insert name mi (moduleMap gr) addModule gr name mi = MGrammar $ Map.insert name mi (moduleMap gr)
-} -}
prependModule (MGrammar mm ms) im@(i,m) = MGrammar (Map.insert i m mm) (im:ms)
emptyMGrammar :: MGrammar a emptyMGrammar :: MGrammar a
emptyMGrammar = mGrammar [] emptyMGrammar = mGrammar []
@@ -237,10 +239,7 @@ abstractOfConcrete gr c = do
_ -> Bad $ render (text "expected concrete" <+> ppIdent c) _ -> Bad $ render (text "expected concrete" <+> ppIdent c)
abstractModOfConcrete :: MGrammar a -> Ident -> Err (ModInfo a) abstractModOfConcrete :: MGrammar a -> Ident -> Err (ModInfo a)
abstractModOfConcrete gr c = do abstractModOfConcrete gr c = lookupModule gr =<< abstractOfConcrete gr c
a <- abstractOfConcrete gr c
lookupModule gr a
-- the canonical file name -- the canonical file name
@@ -253,9 +252,7 @@ lookupModule gr m = case Map.lookup m (moduleMap gr) of
Nothing -> Bad $ render (text "unknown module" <+> ppIdent m <+> text "among" <+> hsep (map (ppIdent . fst) (modules gr))) Nothing -> Bad $ render (text "unknown module" <+> ppIdent m <+> text "among" <+> hsep (map (ppIdent . fst) (modules gr)))
lookupModuleType :: MGrammar a -> Ident -> Err ModuleType lookupModuleType :: MGrammar a -> Ident -> Err ModuleType
lookupModuleType gr m = do lookupModuleType gr m = mtype `fmap` lookupModule gr m
mi <- lookupModule gr m
return $ mtype mi
lookupInfo :: ModInfo a -> Ident -> Err a lookupInfo :: ModInfo a -> Ident -> Err a
lookupInfo mo i = lookupTree showIdent i (jments mo) lookupInfo mo i = lookupTree showIdent i (jments mo)