Morphism.hs revision ee48a7a67da604356b665e51aa7545536a09b737
ce5ff829db5f0bb4f16ad4de150eed4401d6acd5Christian Maeder{-# LANGUAGE MultiParamTypeClasses #-}
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaederModule : $Header$
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian MaederDescription : Symbols and signature morphisms for the CspCASL logic
ce5ff829db5f0bb4f16ad4de150eed4401d6acd5Christian MaederCopyright : (c) Liam O'Reilly, Markus Roggenbach, Swansea University 2008
eca29a7be76eb73944ec19b06eda3d6a9e6e543dChristian MaederLicense : GPLv2 or higher, see LICENSE.txt
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaederMaintainer : csliam@swansea.ac.uk
ce5ff829db5f0bb4f16ad4de150eed4401d6acd5Christian MaederStability : provisional
f3a94a197960e548ecd6520bb768cb0d547457bbChristian MaederPortability : portable
3f63b98c111e5e2bb2cf13795cf6e084a78b0a8dChristian MaederSymbols and signature morphisms for the CspCASL logic
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder ( CspCASLMorphism
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder , CspAddMorphism (..)
15c12a3ac049a4528da05b1017b78145f308aeb0Christian Maeder , mapProcProfile
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder , cspSubsigInclusion
ad270004874ce1d0697fb30d7309f180553bb315Christian Maeder , emptyCspAddMorphism
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder , cspAddMorphismUnion
15c12a3ac049a4528da05b1017b78145f308aeb0Christian Maeder , cspMorphismToCspSymbMap
15c12a3ac049a4528da05b1017b78145f308aeb0Christian Maeder , inducedCspSign
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maederimport qualified CspCASL.LocalTop as LT
2b9022bd5dfb351d1d80f61680336effeccfa23eChristian Maederimport CASL.AS_Basic_CASL (FORMULA, TERM, SORT, SORT_ITEM (..))
cc8b603388a7deb7fb8045db0341f550f8be5844Christian Maederimport CASL.Sign as CASL_Sign
0df692ce8b9293499b2e1768458613a63e7b5cd0Christian Maederimport CASL.Morphism as CASL_Morphism
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maederimport qualified CASL.MapSentence as CASL_MapSen
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maederimport Common.Utils (composeMap, isSingleton)
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maederimport qualified Common.Lib.MapSet as MapSet
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maederimport qualified Common.Lib.Rel as Rel
04dada28736b4a237745e92063d8bdd49a362debChristian Maederimport qualified Data.Map as Map
ce5ff829db5f0bb4f16ad4de150eed4401d6acd5Christian Maederimport qualified Data.Set as Set
cf5149eb4d0faef6272231879c04aa740f5abc2bChristian Maeder{- | This is the second component of a CspCASL signature moprhism, the process
cf5149eb4d0faef6272231879c04aa740f5abc2bChristian Maedername map. We map process name with a profile into new names and
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maedercommunications alphabet. We follow CASL here and instread of mapping to a new
cf5149eb4d0faef6272231879c04aa740f5abc2bChristian Maedername and a new profile, we map just to the new name and the new
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maedercommunications alphabet of the profile. This is because the argument sorts of
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maederthe profile have no chocie they have to be the sorts resultsing from maping
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maederthe original sorts in the profile with the data part map. Note: the
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maedercommunications alphabet of the source profile must be downward closed with
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maederrespect to the CASL signature sub-sort relation (at source) and also the
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maedertarget communications alphabet must be downward closed with respect to the
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian MaederCASL signature sub-sort relation (at target). -}
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maedertype ProcessMap =
04dada28736b4a237745e92063d8bdd49a362debChristian Maeder Map.Map (PROCESS_NAME, ProcProfile) (PROCESS_NAME, CommAlpha)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maedertype ChanMap = Map.Map (CHANNEL_NAME, SORT) CHANNEL_NAME
ccf3de3d66b521a260e5c22d335c64a48e3f0195Christian Maeder-- | CspAddMorphism - This is just the extended part
ccf3de3d66b521a260e5c22d335c64a48e3f0195Christian Maederdata CspAddMorphism = CspAddMorphism
8338fbf3cfb9cf981261d893286f070bd9fa17efChristian Maeder {- Note that when applying the CspAddMorphism to process names or channel
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder names, if the name is not in the map in the morphism, then the
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder application is the identity function. Thus empty maps are used to form
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder the empty morphism and the identity morphism. -}
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder { channelMap :: ChanMap
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder , processMap :: ProcessMap
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder } deriving (Eq, Ord, Show)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder-- | The empty CspAddMorphism.
42c01284bba8d7c8d995c8dfb96ace57d28ed1bcTill MossakowskiemptyCspAddMorphism :: CspAddMorphism
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaederemptyCspAddMorphism = CspAddMorphism
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder {- Note that when applying the CspAddMorphism to process names or
2986838ec286d67e7c199e7ea81e7364ca36ad25Christian Maeder channel names, if the name is not in the map in the morphism,
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder then the application is the identity function. Thus empty maps
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder are used to form the empty morphism. -}
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder{- | Given two signatures (the first being a sub signature of the second
7dec34aee2b609b9535c48d060e0f7baf3536457Christian Maederaccording to CspCASL.SignCSP.isCspCASLSubSig) compute the inclusion morphism. -}
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedercspSubsigInclusion :: CspCASLSign -> CspCASLSign -> Result CspCASLMorphism
e76e6a43f51438215737d6fc176c89da05bb86daChristian MaedercspSubsigInclusion = CASL_Morphism.sigInclusion emptyCspAddMorphism
3f63b98c111e5e2bb2cf13795cf6e084a78b0a8dChristian Maeder{- We use the empty morphism as it also represents the identity, thus this
0df692ce8b9293499b2e1768458613a63e7b5cd0Christian Maederwill embed channel names and process names properly. -}
0df692ce8b9293499b2e1768458613a63e7b5cd0Christian Maeder-- | lookup a typed channel
0b14ccc700093e203914052bf6aceda3164af730Christian MaedermapChan :: Sort_map -> ChanMap -> (CHANNEL_NAME, SORT) -> (CHANNEL_NAME, SORT)
0b14ccc700093e203914052bf6aceda3164af730Christian MaedermapChan sm cm p@(c, s) = (Map.findWithDefault c p cm, mapSort sm s)
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder-- | Apply a signature morphism to a channel name
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapChannel :: Morphism f CspSign CspAddMorphism -> (CHANNEL_NAME, SORT)
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder -> (CHANNEL_NAME, SORT)
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian MaedermapChannel mor = mapChan (sort_map mor) $ channelMap $ extended_map mor
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapCommTypeAux :: Sort_map -> ChanMap -> CommType -> CommType
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedermapCommTypeAux sm cm ct = case ct of
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder CommTypeSort s -> CommTypeSort $ mapSort sm s
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder CommTypeChan (TypedChanName c s) -> let (d, t) = mapChan sm cm (c, s) in
d48085f765fca838c1d972d2123601997174583dChristian Maeder CommTypeChan $ TypedChanName d t
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder-- | Apply a signature morphism to a CommType
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapCommType :: Morphism f CspSign CspAddMorphism -> CommType -> CommType
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapCommType mor = mapCommTypeAux (sort_map mor) (channelMap $ extended_map mor)
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapCommAlphaAux :: Sort_map -> ChanMap -> CommAlpha -> CommAlpha
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapCommAlphaAux sm = Set.map . mapCommTypeAux sm
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder-- | Apply a signature morphism to a CommAlpha
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedermapCommAlpha :: Morphism f CspSign CspAddMorphism -> CommAlpha -> CommAlpha
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedermapCommAlpha = Set.map . mapCommType
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedermapProcProfile :: Sort_map -> ChanMap -> ProcProfile -> ProcProfile
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian MaedermapProcProfile sm cm (ProcProfile sl cs) =
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder ProcProfile (map (mapSort sm) sl) $ mapCommAlphaAux sm cm cs
793945d4ac7c0f22760589c87af8e71427c76118Christian MaedermapProcId :: Sort_map -> ChanMap -> ProcessMap
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder -> (PROCESS_NAME, ProcProfile) -> (PROCESS_NAME, ProcProfile)
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedermapProcId sm cm pm (i, p) = let
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder n@(ProcProfile args _) = mapProcProfile sm cm p
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder in case Map.lookup (i, p) pm of
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder Nothing -> (i, n)
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder Just (j, alpha) -> (j, ProcProfile args alpha)
793945d4ac7c0f22760589c87af8e71427c76118Christian MaedermapProcess :: Morphism f CspSign CspAddMorphism
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder -> (PROCESS_NAME, ProcProfile) -> (PROCESS_NAME, ProcProfile)
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian MaedermapProcess mor = let em = extended_map mor in
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder mapProcId (sort_map mor) (channelMap em) $ processMap em
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder-- | Compose two CspAddMorphisms
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian MaedercomposeCspAddMorphism :: Morphism f CspSign CspAddMorphism
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder -> Morphism f CspSign CspAddMorphism -> Result CspAddMorphism
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian MaedercomposeCspAddMorphism m1 m2 = let
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder sMap1 = sort_map m1
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder sMap2 = sort_map m2
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder sMap = composeMap (MapSet.setToMap $ sortSet src) sMap1 sMap2
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder src = msource m1
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder cSrc = extendedInfo src
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder cMap = MapSet.foldWithKey ( \ c s ->
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder let p = (c, s)
0f0aa53f11a0d1ab08c76428b9de73db5b17c977Christian Maeder ni = fst $ mapChannel m2 $ mapChannel m1 p
793945d4ac7c0f22760589c87af8e71427c76118Christian Maeder in if c == ni then id else Map.insert p ni)
0b14ccc700093e203914052bf6aceda3164af730Christian Maeder pMap = MapSet.foldWithKey ( \ p pr@(ProcProfile _ a) ->
cc8b603388a7deb7fb8045db0341f550f8be5844Christian Maeder let pp = (p, pr)
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder (ni, ProcProfile _ na) =
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian Maeder mapProcess m2 $ mapProcess m1 pp
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder oa = mapCommAlphaAux sMap cMap a
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder in if p == ni && oa == na then id else
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder in return emptyCspAddMorphism
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder { channelMap = cMap
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder , processMap = pMap }
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder{- | A CspCASLMorphism is a CASL Morphism with the extended_map to be a
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaederCspAddMorphism. -}
0b14ccc700093e203914052bf6aceda3164af730Christian Maedertype CspCASLMorphism = CASL_Morphism.Morphism CspSen CspSign CspAddMorphism
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder{- | Check if a CspCASL signature morphism has the refl property i.e.,
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maedersigma(s1) <= sigma(s2) implies s1 <= s2 for all s1, s2 in S -}
15c12a3ac049a4528da05b1017b78145f308aeb0Christian MaedercheckReflCondition :: Morphism f CspSign CspAddMorphism -> Result ()
f4741f6b7da52b5417899c8fcbe4349b920b006eChristian MaedercheckReflCondition mor =
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder let sig = msource mor
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder sig' = mtarget mor
cc8b603388a7deb7fb8045db0341f550f8be5844Christian Maeder sm = sort_map mor
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder rel = sortRel sig
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder rel' = sortRel sig'
1d589334ba6b4a4cbfb35307a7a732261e77b0cdChristian Maeder allPairs = LT.cartesian $ sortSet sig
cc8b603388a7deb7fb8045db0341f550f8be5844Christian Maeder failures = Set.filter (not . test) allPairs
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder test (s1, s2) = if Rel.path (mapSort sm s1) (mapSort sm s2) rel' ||
cc8b603388a7deb7fb8045db0341f550f8be5844Christian Maeder mapSort sm s1 == mapSort sm s2
f626b1acbe874a48143a6f8d6246bf9d7a055ffbChristian Maeder then Rel.path s1 s2 rel || s1 == s2
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder in if Set.null failures
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder then return ()
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder else let produceDiag (s1, s2) =
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder let x = (mapSort sm s1)
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder y = (mapSort sm s2)
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder in Diag Error
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder ("CSP-CASL Signature Morphism Refl Property Violated:\n'"
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder ++ showDoc (Symbol x $ SubsortAsItemType y) "' but not\n'"
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder ++ showDoc (Symbol s1 $ SubsortAsItemType s2) "'")
20fe556546c9277cf017931a07d90add61f199d9Christian Maeder allDiags = map produceDiag $ Set.toList failures
c2dead95fafd7ca36d06ddf07606a1292ead6d8aChristian Maeder in Result allDiags Nothing -- failure with error messages
4ef2a978e66e2246ff0b7f00c77deb7aabb28b8eChristian Maeder{- | Check if a CspCASL signature morphism has the weak non extension property
3f63b98c111e5e2bb2cf13795cf6e084a78b0a8dChristian Maedersigma(s1) <= u' and sigma(s2) <= u' implies there exists t in S with
c9b711a46e5138b2742727817c8071960e673073Christian Maeders1 <= t and s2 <= t and sigma(t) <= u' for all s1,s2 in S and u' in S' -}
c9b711a46e5138b2742727817c8071960e673073Christian MaedercheckWNECondition :: Morphism f CspSign CspAddMorphism -> Result ()
c438c79d00fc438f99627e612498744bdc0d0c89Christian MaedercheckWNECondition mor =
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder let sig = msource mor
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder sig' = mtarget mor
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder sm = sort_map mor
10397bcc134edbcfbe3ae2c7ea4c6080036aae22Christian Maeder rel' = sortRel sig'
59c036af82aff7fbe074455dad50477b7878e2d8Christian Maeder supers s signature = Set.insert s $ supersortsOf s signature
81946e2b3f6dde6167f48769bd02c7a634736856Christian Maeder allPairsInSource = LT.cartesian $ sortSet sig
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder commonSuperSortsInTarget s1 s2 = Set.intersection
59c036af82aff7fbe074455dad50477b7878e2d8Christian Maeder (supers (mapSort sm s1) sig')
59c036af82aff7fbe074455dad50477b7878e2d8Christian Maeder (supers (mapSort sm s2) sig')
c438c79d00fc438f99627e612498744bdc0d0c89Christian Maeder {- Candidates are triples (s1,s2,u') such that
Set.map (\ u' -> (s1, s2, u')) (commonSuperSortsInTarget s1 s2)
let possibleWitnesses = Set.intersection (supers s1 sig) (supers s2 sig)
test t = Rel.path (mapSort sm t) u' rel' || mapSort sm t == u'
failures = Set.filter (not . testCandidate) allCandidateTripples
in if Set.null failures
allDiags = map produceDiag $ Set.toList failures
delChan (n, s) m = MapSet.delete n s m
uc1 = foldr delChan (chans s1) $ Map.keys chan1
uc2 = foldr delChan (chans s2) $ Map.keys chan2
uc = MapSet.union uc1 uc2
delProc (n, p) m = MapSet.delete n p m
up1 = foldr delProc (procSet s1) $ Map.keys proc1
up2 = foldr delProc (procSet s2) $ Map.keys proc2
up = MapSet.union up1 up2
++ if isSingleton s then showDoc (Set.findMin s) "" else showDoc s ""
case Map.lookup isc m of
Nothing -> (ds, Map.insert isc j m)
(Map.toList chan2 ++ concatMap ( \ (c, ts) -> map
( \ s -> ((c, s), c)) ts) (MapSet.toList uc))
case Map.lookup isc m of
Nothing -> (ds, Map.insert isc j m)
(Map.toList proc2 ++ concatMap ( \ (p, pts) -> map
(MapSet.toList up))
-> Map.Map CspSymbol CspSymbol
chanSymMap = MapSet.foldWithKey
Map.insert (toChanSymbol p) $ toChanSymbol q)
Map.empty $ chans src
procSymMap = MapSet.foldWithKey
Map.insert (toProcSymbol p) $ toProcSymbol q)
Map.empty $ procSet src
in Map.union chanSymMap procSymMap
cspMorphismToCspSymbMap :: CspCASLMorphism -> Map.Map CspSymbol CspSymbol
$ Map.toList $ morphismToSymbMap mor)
instance CASL_Morphism.MorphismExtension CspSign CspAddMorphism
inducedChanMap sm cm = MapSet.foldWithKey
inducedProcMap sm cm pm = MapSet.foldWithKey
mapSort' = CASL_MapSen.mapSrt mor
error "CspCASL.Morphism.mapRenaming: Unexpected Renaming"
{- | Apply a signature morphism to a CASL TERM (for CspCASL only, i.e. a CASL
{- | Apply a signature morphism to a CASL FORMULA (for CspCASL only, i.e. a CASL