-- -- Copyright (C) 2005 Stefan Wehr - http://www.stefanwehr.de -- -- This program is free software; you can redistribute it and/or -- modify it under the terms of the GNU General Public License as -- published by the Free Software Foundation; either version 2 of -- the License, or (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -- General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA -- 02111-1307, USA. -- module Phrac.IdMap where import qualified Phrac.Map as Map import Phrac.AbstractSyntax import Phrac.Pretty data IdMap a b c d e f = IdMap { typeVarMap :: Map.Map TypeVarId a, typeMap :: Map.Map TypeId b, valMap :: Map.Map ValId c, dataMap :: Map.Map DataId d, classMap :: Map.Map ClassId e, assocTypeMap :: Map.Map AssocTypeId f } emptyIdMap = IdMap e e e e e e where e = Map.empty elemsIdMap m = (Map.elems (typeVarMap m), Map.elems (typeMap m), Map.elems (valMap m), Map.elems (dataMap m), Map.elems (classMap m), Map.elems (assocTypeMap m)) mapIdMap :: ((a -> a'), (b -> b'), (c -> c'), (d -> d'), (e -> e'), (f -> f')) -> IdMap a b c d e f -> IdMap a' b' c' d' e' f' mapIdMap (a, b, c, d, e, f) m = IdMap { typeVarMap = Map.map a (typeVarMap m), typeMap = Map.map b (typeMap m), valMap = Map.map c (valMap m), dataMap = Map.map d (dataMap m), classMap = Map.map e (classMap m), assocTypeMap = Map.map f (assocTypeMap m) } insertTypeVar k v m = m { typeVarMap = Map.insert k v (typeVarMap m) } insertTypeVars kvs m = m { typeVarMap = foldr (\ (k,v) m -> Map.insert k v m) (typeVarMap m) kvs } insertType k v m = m { typeMap = Map.insert k v (typeMap m) } insertVal k v m = m { valMap = Map.insert k v (valMap m) } insertData k v m = m { dataMap = Map.insert k v (dataMap m) } insertClass k v m = m { classMap = Map.insert k v (classMap m) } insertAssocType k v m = m { assocTypeMap = Map.insert k v (assocTypeMap m) } saveLookup k m = case Map.lookup k m of Just v -> return v Nothing -> fail ("IdMap.saveLookup failed. key: " ++ show k) lookupTypeVar k m = saveLookup k (typeVarMap m) lookupType k m = saveLookup k (typeMap m) lookupVal k m = saveLookup k (valMap m) lookupData k m = saveLookup k (dataMap m) lookupClass k m = saveLookup k (classMap m) lookupAssocType k m = saveLookup k (assocTypeMap m) dumpIdMap :: (Pretty a, Pretty b, Pretty c, Pretty d, Pretty e, Pretty f) => IdMap a b c d e f -> Doc dumpIdMap m = text "{typeVarMap:" <+> dumpMap (typeVarMap m) $$ text "typeMap:" <+> dumpMap (typeMap m) $$ text "valMap:" <+> dumpMap (valMap m) $$ text "dataMap:" <+> dumpMap (dataMap m) $$ text "classMap:" <+> dumpMap (classMap m) $$ text "assocTypeMap:" <+> dumpMap (assocTypeMap m) <> text"}"