Morphism.hs revision 521e1648b2c66064c41e9ac47bcd510356ed2355
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettModule : $Header$
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettDescription : Morphisms for Maude
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettCopyright : (c) Martin Kuehl, Uni Bremen 2008
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettMaintainer : mkhl@informatik.uni-bremen.de
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettStability : experimental
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettPortability : portable
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy GimblettDefinition of morphisms for Maude.
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett Morphism(..),
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett fromSignRenamings,
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett fromSignsRenamings,
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett createInclMorph,
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett extendMorphismSorts,
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblett addQualification
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblettimport qualified Maude.Sign as Sign
aa0d5f8be9950e788884f7431cf4cb7bee74788cAndy Gimblettimport qualified Data.Set as Set
9ebbce450fb242e1a346f9f89367d8c46fcb2ec8Andy Gimblettimport qualified Data.Map as Map
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblettimport qualified Common.Result as Result
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblettimport Common.Doc as Doc hiding (empty)
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblettimport Common.Id (mkSimpleId)
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimbletttype Profile = ([Qid], Qid)
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimbletttype QidMap = Map.Map Qid Qid
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimbletttype SortMap = QidMap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimbletttype OpMap = Map.Map Qid (Map.Map Profile (Qid, Profile))
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimbletttype LabelMap = QidMap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblettdata Morphism = Morphism {
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett source :: Sign,
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett target :: Sign,
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett sortMap :: SortMap,
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett opMap :: OpMap,
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett labelMap :: LabelMap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett } deriving (Show, Ord, Eq)
d40dd10adffcf341489a1310092fcc99de75f225Andy Gimblettinstance Pretty Morphism where
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett pretty m = Doc.text $ printMorphism (sortMap m) (opMap m) (labelMap m)
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett ++ "\n\nTarget:\n" ++ printSign (Sign.sorts sign)
d40dd10adffcf341489a1310092fcc99de75f225Andy Gimblett where sign = target m
d40dd10adffcf341489a1310092fcc99de75f225Andy Gimblett-- | create a Morphism from an initial signature and a list of Renamings
d40dd10adffcf341489a1310092fcc99de75f225Andy GimblettfromSignRenamings :: Sign -> [Renaming] -> Morphism
d40dd10adffcf341489a1310092fcc99de75f225Andy GimblettfromSignRenamings sg rnms = applyRenamings (createInclMorph sg sg) rnms
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenamings :: Morphism -> [Renaming] -> Morphism
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenamings m rnms = foldr applyRenaming2 (foldr applyRenaming1 m rnms) rnms
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett-- | Rename the signature with the operator renaming and creates a morphism
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett-- with only the operator map. Sort renamings have to be applied to this
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenaming1 :: Renaming -> Morphism -> Morphism
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenaming1 rnm mor = let
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett tgt = target mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett omap = opMap mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett in case rnm of
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett OpRenaming1 from (To to ats) -> let
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett a = getName from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett b = getName to
d40dd10adffcf341489a1310092fcc99de75f225Andy Gimblett target = Sign.renameOp a b ats tgt,
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett opMap = allProfilesRenaming a b tgt omap
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett OpRenaming2 from ar co (To to ats) -> let
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett a = getName from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett b = getName to
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett ar' = map getName ar
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett co' = getName co
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett p = (ar', co')
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett target = Sign.renameOpProfile a ar' co' b ats tgt,
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett opMap = Map.insertWith (Map.union) a (Map.insert p (b, p) Map.empty) omap
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett-- | Rename a signature with the sort and label renamings, and modify
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett-- the given morphism by renaming the sorts in the profiles.
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenaming2 :: Renaming -> Morphism -> Morphism
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettapplyRenaming2 rnm mor = let
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett tgt = target mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett smap = sortMap mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett omap = opMap mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett lmap = labelMap mor
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett in case rnm of
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett SortRenaming from to -> let
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett a = getName from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett b = getName to
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett target = Sign.renameSort a b tgt,
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett sortMap = Map.insert a b smap,
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett opMap = renameSortOpMap a b omap
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett LabelRenaming from to -> let
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett a = getName from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett b = getName to
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett labelMap = Map.insert a b lmap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett-- | Rename sorts in the profiles of an operator map.
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettrenameSortOpMap :: Qid -> Qid -> OpMap -> OpMap
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy GimblettrenameSortOpMap from to = Map.map f
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett where f = \ x -> Map.map g x
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett where g = \ (q, (ar, co)) -> (q, (map h ar, if co == from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett where h = \ x -> if x == from
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettallProfilesRenaming :: Qid -> Qid -> Sign -> OpMap -> OpMap
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettallProfilesRenaming from to sg om = om'
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett where om_sg = Sign.ops sg
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett om' = if Map.member from om_sg
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett then Map.insert from (allProfilesRenamingAux to $ fromJust $ Map.lookup from om_sg) om
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettallProfilesRenamingAux :: Qid -> Sign.OpDeclSet -> Map.Map Profile (Qid, Profile)
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettallProfilesRenamingAux q = Set.fold (f q) Map.empty
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett where f = \ to (x, y, _) m -> Map.insert (x, y) (to, (x, y)) m
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett-- | extract a Symbol Map from a Morphism
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettsymbolMap :: Morphism -> SymbolMap
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy GimblettsymbolMap mor = Map.unions [(sortMap2symbolMap $ sortMap mor),
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett (opMap2symbolMap $ opMap mor),
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett (labelMap2symbolMap $ labelMap mor)]
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettsortMap2symbolMap :: QidMap -> SymbolMap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettsortMap2symbolMap = Map.fromList . map f . Map.toList
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett where f = \ (x, y) -> (Sort x, Sort y)
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettopMap2symbolMap :: OpMap -> SymbolMap
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettopMap2symbolMap = Map.fromList . map h . map f . Map.toList
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett where f = \ (x, y) -> map (g x) $ Map.toList y
2cf5a456da8bb3a2bbb695414d8304426e3bd277Andy Gimblett where g = \ q1 ((ar1, co1), (q2, (ar2, co2))) ->
ae6d8241c2ce8132a6e22d9f854edb612c2f637dAndy Gimblett (Operator q1 ar1 co1, Operator q2 ar2 co2)
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett h = \ [a] -> a
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy Gimblett-- type OpMap = Map.Map Qid (Map.Map Profile (Qid, Profile))
b5301fa0ef9e88a488e5cfe8c395a05c2f6884d3Andy GimblettlabelMap2symbolMap :: QidMap -> SymbolMap
sortMap = Map.insert a b smap,
labelMap = Map.insert a b lmap
empty = identity Sign.empty
sortMap = Map.empty,
opMap = Map.empty,
labelMap = Map.empty
-- TODO: Ops don't have labels, so Signs don't have labels. so we can ignore the labelMap. Is that correct?
in if x == y then id else Map.insert x y
compose'map mp items = if Map.null (mp g)
-- TODO: Ops don't have labels, so Signs don't have labels. so we can ignore the labelMap. Is that correct?
legal'source = Sign.isLegal src
legal'target = Sign.isLegal tgt
-- TODO: Implement Morphism.isInclusion.
null'sortMap = Map.null (sortMap mor)
null'opMap = Map.null (opMap mor)
null'labelMap = Map.null (labelMap mor)
source = Sign.union (source m1) $ source m2,
target = Sign.union (target m1) $ target m2,
sortMap = Map.union (sortMap m1) $ sortMap m2,
labelMap = Map.union (labelMap m1) $ labelMap m2
f = \ x -> if Map.member x sm
then fromJust $ Map.lookup x sm
target = Sign.renameListSort (createRenaming sym syms) tgt,
source = Sign.renameListSort (createRenaming sym syms) src,