Morphism.hs revision 4fb19f237193a3bd6778f8aee3b6dd8da5856665
967e5f3c25249c779575864692935627004d3f9eChristian MaederModule : $Header$
967e5f3c25249c779575864692935627004d3f9eChristian MaederCopyright : (c) Christian Maeder and Uni Bremen 2002-2003
75a6279dbae159d018ef812185416cf6df386c10Till MossakowskiLicence : similar to LGPL, see HetCATS/LICENCE.txt or LIZENZ.txt
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian MaederMaintainer : maeder@tzi.de
967e5f3c25249c779575864692935627004d3f9eChristian MaederStability : provisional
89054b2b95a3f92e78324dc852f3d34704e2ca49Christian MaederPortability : portable
967e5f3c25249c779575864692935627004d3f9eChristian MaederMorphism on 'Env' (as for CASL)
fd896e2068ad7e50aed66ac18c3720ea7ff2619fChristian Maederimport qualified Common.Lib.Map as Map
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maedertype FunMap = Map.Map (Id, TypeScheme) (Id, TypeScheme)
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maederdata Morphism = Morphism { msource :: Env
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder , mtarget :: Env
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder , classIdMap :: IdMap -- ignore
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder , typeIdMap :: IdMap
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder , funMap :: FunMap }
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder deriving (Eq, Show)
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maederinstance PrettyPrint Morphism where
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder printText0 ga m =
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder let tm = typeIdMap m
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder fm = funMap m
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder ds = Map.foldWithKey ( \ (i, s) (j, t) l ->
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder (printText0 ga i <+> colon <+> printText0 ga s
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder <+> text mapsTo <+>
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder printText0 ga j <+> colon <+> printText0 ga t) : l)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder in (if Map.isEmpty tm then empty
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder else text (typeS ++ sS) <+> printText0 ga tm)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder $$ (if Map.isEmpty fm then empty
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder else text (opS ++ sS) <+> fsep (punctuate comma ds))
0f67ca7b0c738a28f6688ba6e96d44d7c14af611Christian Maeder $$ nest 1 colon <+> braces (printText0 ga $ msource m)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder $$ nest 1 (text mapsTo)
0f67ca7b0c738a28f6688ba6e96d44d7c14af611Christian Maeder <+> braces (printText0 ga $ mtarget m)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian MaedermapTypeScheme :: IdMap -> TypeScheme -> TypeScheme
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedermapTypeScheme im = mapTypeOfScheme $ mapType im
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian MaedermapSen :: Morphism -> Term -> Term
0f67ca7b0c738a28f6688ba6e96d44d7c14af611Christian MaedermapSen m = let im = typeIdMap m in
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder mapTerm (mapFunSym im (funMap m), mapType im)
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian MaedermapDataEntry :: Morphism -> DataEntry -> DataEntry
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian MaedermapDataEntry m (DataEntry tm i k args alts) =
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder let tim = compIdMap tm $ typeIdMap m
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder in DataEntry tim i k args $ map
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian Maeder (mapAlt m tim args (Map.findWithDefault i i tim, args, star)) alts
2a598ff0c1b7b51c33aee7029b43bc5cfcbea6b8Christian MaedermapAlt :: Morphism -> IdMap -> [TypeArg] -> DataPat -> AltDefn -> AltDefn
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian MaedermapAlt m tm args dt c@(Construct mi ts p sels) =
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder let sc = TypeScheme args
d17834302eaa101395b4b806cd73670fd864445fChristian Maeder (getConstrType dt p (map (mapType tm) ts)) []
d17834302eaa101395b4b806cd73670fd864445fChristian Maeder (j, TypeScheme _ ty _) =
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder mapFunSym (typeIdMap m) (funMap m) (i, sc)
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder in Construct (Just j) ts (getPartiality ts ty) sels
5e26bfc8d7b18cf3a3fa7b919b4450fb669f37a5Christian Maeder -- do not change (unused) selectors
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian MaedermapSentence :: Morphism -> Sentence -> Result Sentence
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian MaedermapSentence m s = return $ case s of
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder Formula t -> Formula $ mapSen m t
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder DatatypeSen td -> DatatypeSen $ map (mapDataEntry m) td
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder ProgEqSen i sc pe ->
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder let tm = typeIdMap m
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder fm = funMap m
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder f = mapFunSym tm fm
e7ce154edb906685b3fa7f6c0a764e18a4658068Christian Maeder (ni, nsc) = f (i, sc)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder in ProgEqSen ni nsc $ mapEq (f, mapType tm) pe
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedermapFunSym :: IdMap -> FunMap -> (Id, TypeScheme) -> (Id, TypeScheme)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedermapFunSym im fm (i, sc) =
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder let (j, sc2) = Map.findWithDefault (i, mapTypeScheme im sc)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder (i, sc) fm in
967e5f3c25249c779575864692935627004d3f9eChristian MaedermkMorphism :: Env -> Env -> Morphism
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian MaedermkMorphism e1 e2 = Morphism e1 e2 Map.empty Map.empty Map.empty
83814002b4922114cbe7e9ba728472a0bf44aac5Christian MaederembedMorphism :: Env -> Env -> Morphism
a578ec30cded5e396a7ce9a3b469e8cd3a88246aChristian MaederembedMorphism = mkMorphism
967e5f3c25249c779575864692935627004d3f9eChristian MaederideMor :: Env -> Morphism
83814002b4922114cbe7e9ba728472a0bf44aac5Christian MaederideMor e = embedMorphism e e
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian MaedercompIdMap :: IdMap -> IdMap -> IdMap
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian MaedercompIdMap im1 im2 = Map.foldWithKey ( \ i j ->
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder Map.insert i $ Map.findWithDefault j j im2)
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian MaedercompMor :: Morphism -> Morphism -> Maybe Morphism
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian MaedercompMor m1 m2 =
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder if isSubEnv (mtarget m1) (msource m2) &&
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder isSubEnv (msource m2) (mtarget m1) then
a578ec30cded5e396a7ce9a3b469e8cd3a88246aChristian Maeder let tm2 = typeIdMap m2
a578ec30cded5e396a7ce9a3b469e8cd3a88246aChristian Maeder fm2 = funMap m2 in Just
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder (mkMorphism (msource m1) (mtarget m2))
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder { classIdMap = compIdMap (classIdMap m1) $ classIdMap m2
e1839fb37a3a2ccd457464cb0dcc5efd466dbe22Christian Maeder , typeIdMap = compIdMap (typeIdMap m1) tm2
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder , funMap = Map.foldWithKey ( \ p1 p2 ->
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder $ mapFunSym tm2 fm2 p2) fm2 $ funMap m1
bfa9e03532243ceb487f0384d0f6a447f1ce7670Till MossakowskiinclusionMor :: Env -> Env -> Result Morphism
7221c71b38c871ce66eee4537cb681d468308dfbChristian MaederinclusionMor e1 e2 =
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder if isSubEnv e1 e2
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder then return (embedMorphism e1 e2)
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder else pplain_error (ideMor initialEnv)
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder (ptext "Attempt to construct inclusion between non-subsignatures:"
e76e6a43f51438215737d6fc176c89da05bb86daChristian Maeder $$ ptext "Signature 1:" $$ printText e1
e76e6a43f51438215737d6fc176c89da05bb86daChristian Maeder $$ ptext "Signature 2:" $$ printText e2)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian MaedersymbMapToMorphism :: Env -> Env -> SymbolMap -> Result Morphism
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder-- consider partial symbol map
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian MaedersymbMapToMorphism sigma1 sigma2 smap = do
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder type_map1 <- Map.foldWithKey myIdMap (return Map.empty) $ typeMap sigma1
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder fun_map1 <- Map.foldWithKey myAsMap (return Map.empty) $ assumps sigma1
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder return (mkMorphism sigma1 sigma2)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder { typeIdMap = type_map1
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder , funMap = fun_map1}
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder myIdMap i k m = do
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder sym <- maybeToResult nullPos
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder ("symbMapToMorphism - Could not map type "++showId i "")
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder $ Map.lookup (Symbol { symName = i
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder , symType = TypeAsItemType
e817ea5134dced9e0bcce1a9d6b8fe4f81d36e56Christian Maeder , symEnv = sigma1 }) smap
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder return (Map.insert i (symName sym) m1)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder myAsMap i (OpInfos ots) m = foldr (insFun i) m ots
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder insFun i ot m = do
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder let osc = opType ot
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder sym <- maybeToResult nullPos
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder ("symbMapToMorphism - Could not map op "++showId i "")
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder $ Map.lookup (Symbol { symName = i
e817ea5134dced9e0bcce1a9d6b8fe4f81d36e56Christian Maeder , symType = OpAsItemType osc
e817ea5134dced9e0bcce1a9d6b8fe4f81d36e56Christian Maeder , symEnv = sigma1 }) smap
dea4c92f0c061d589c542d0640a18dab36dfbb46Christian Maeder k <- case symType sym of
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder OpAsItemType sc -> return sc
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder _ -> plain_error (osc)
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder ("symbMapToMorphism - Wrong result symbol type for op"
0a8ea95bcf0e3f84fed0b725c049ec2a956a4a28Christian Maeder ++showId i "") nullPos
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder return (Map.insert (i, osc) (symName sym, k) m1)
967e5f3c25249c779575864692935627004d3f9eChristian MaederlegalEnv :: Env -> Bool
967e5f3c25249c779575864692935627004d3f9eChristian MaederlegalEnv _ = True -- maybe a closure test?
967e5f3c25249c779575864692935627004d3f9eChristian MaederlegalMor :: Morphism -> Bool
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian MaederlegalMor m = let s = msource m
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder t = mtarget m
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder ts = typeIdMap m
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder fs = funMap m
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder all (`elem` (Map.keys $ typeMap s))
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder && all (`elem` (Map.keys $ typeMap t))
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder && all ((`elem` (Map.keys $ assumps s)) . fst)
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian Maeder && all ((`elem` (Map.keys $ assumps t)) . fst)
967e5f3c25249c779575864692935627004d3f9eChristian MaedermorphismUnion :: Morphism -> Morphism -> Result Morphism
7221c71b38c871ce66eee4537cb681d468308dfbChristian MaedermorphismUnion m1 m2 =
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder do s <- merge (msource m1) $ msource m2
ee9eddfa6953868fd6fbaff0d9ff68675a13675aChristian Maeder t <- merge (mtarget m1) $ mtarget m2
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder tm <- foldr ( \ (i, j) rm ->
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder Nothing -> return $ Map.insert i j m
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder Just k -> if j == k then return m
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder Result [Diag Error
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ("incompatible mapping of type id: " ++
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder showId i " to: " ++ showId j " and: "
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ++ showId k "") $ posOfId i] $ Just ()
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder (return $ typeIdMap m1) $ Map.toList $ typeIdMap m2
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder fm <- foldr ( \ (isc@(i, sc), jsc@(j, sc1)) rm -> do
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder let nsc = expand (typeMap t) sc1
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder nisc = (i, expand (typeMap s) sc)
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder Nothing -> return $ Map.insert nisc
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder Just ksc@(k, sc2) -> if j == k &&
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder then return m
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder Result [Diag Error
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ("incompatible mapping of op: " ++
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder showFun isc " to: " ++ showFun jsc " and: "
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder ++ showFun ksc "") $ posOfId i] $ Just ()
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder (return Map.empty) (Map.toList (funMap m1) ++
7221c71b38c871ce66eee4537cb681d468308dfbChristian Maeder return (mkMorphism s t)
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder { typeIdMap = tm
83814002b4922114cbe7e9ba728472a0bf44aac5Christian Maeder , funMap = fm }
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowFun :: (Id, TypeScheme) -> ShowS
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian MaedershowFun (i, ty) = showId i . (" : " ++) . showPretty ty
83814002b4922114cbe7e9ba728472a0bf44aac5Christian MaedermorphismToSymbMap :: Morphism -> SymbolMap
dedabc954aa15f6ad0764472a9434dc6dafe3db2Christian MaedermorphismToSymbMap mor =
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder src = msource mor
97ee7048e63953c5617342ce38c30cbcb35cc0beChristian Maeder tar = mtarget mor
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder tm = typeIdMap mor
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder typeSymMap = Map.foldWithKey ( \ i ti ->
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder k = typeKind ti
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder in Map.insert (idToTypeSymbol src i k)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder $ idToTypeSymbol tar j k) Map.empty $ typeMap src
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder ( \ i (OpInfos l) m ->
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder foldr ( \ oi ->
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder let ty = opType oi
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder (j, t2) = mapFunSym
4fb19f237193a3bd6778f8aee3b6dd8da5856665Christian Maeder tm (funMap mor) (i, ty)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder in Map.insert (idToOpSymbol src i ty)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder (idToOpSymbol tar j t2)) m l)
07b72edb610ee53b4832d132e96b0a3d8423f8ebChristian Maeder typeSymMap $ assumps src