Symbol.hs revision 0a58641cb9f0c51d02626a826acde9785b4f4a36
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder{-# LANGUAGE DeriveDataTypeable #-}
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederDescription : semantic csp-casl symbols
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederCopyright : (c) Christian Maeder, DFKI GmbH 2011
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederLicense : GPLv2 or higher, see LICENSE.txt
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederMaintainer : Christian.Maeder@dfki.de
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederStability : provisional
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederPortability : portable
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maederimport qualified Common.Lib.MapSet as MapSet
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederimport qualified Data.Map as Map
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederimport qualified Data.Set as Set
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederdata CspSymbType
4c7f058cdd19ce67b2b5d4b7f69703d0f8a21e38Christian Maeder = CaslSymbType SymbType
4c7f058cdd19ce67b2b5d4b7f69703d0f8a21e38Christian Maeder | ProcAsItemType ProcProfile
4c7f058cdd19ce67b2b5d4b7f69703d0f8a21e38Christian Maeder | ChanAsItemType Id -- the SORT
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder deriving (Show, Eq, Ord, Typeable, Data)
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maederinstance Pretty CspSymbType where
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder pretty (CaslSymbType st) = pretty $ symbolKind st
67869d63d1725c79e4c07b51acd466a31932b275Christian Maeder pretty (ProcAsItemType _) = text "process"
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder pretty (ChanAsItemType _) = text "channel"
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederdata CspSymbol = CspSymbol {cspSymName :: Id, cspSymbType :: CspSymbType}
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder deriving (Show, Eq, Ord, Typeable, Data)
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederdata CspRawSymbol = ACspSymbol CspSymbol | CspKindedSymb CspSymbKind Id
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder deriving (Show, Eq, Ord, Typeable, Data)
65dce48b81f69e11a36bf1051314a845299446e1Christian MaederrawId :: CspRawSymbol -> Id
64e1905404e5135e98a26d2ab4150b6764956576Christian MaederrawId r = case r of
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder ACspSymbol s -> cspSymName s
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder CspKindedSymb _ i -> i
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maederinstance Pretty CspSymbol where
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder pretty (CspSymbol i t) = case t of
67869d63d1725c79e4c07b51acd466a31932b275Christian Maeder ProcAsItemType p -> keyword processS <+> pretty i <+> pretty p
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder ChanAsItemType s -> keyword channelS <+> pretty i <+> colon <+> pretty s
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder CaslSymbType c -> pretty $ Symbol i c
64e1905404e5135e98a26d2ab4150b6764956576Christian Maederinstance GetRange CspSymbol where
62eaa2fb831613d8a6e59687f83a45be1041ab17Christian Maeder getRange (CspSymbol i _) = getRange i
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maederinstance Pretty CspRawSymbol where
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder pretty r = case r of
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder ACspSymbol s -> pretty s
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder CspKindedSymb k i -> pretty k <+> pretty i
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maederinstance GetRange CspRawSymbol where
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder getRange r = case r of
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder ACspSymbol s -> getRange s
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder CspKindedSymb _ i -> getRange i
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedercspCheckSymbList :: [CspSymbMap] -> [Diagnosis]
67869d63d1725c79e4c07b51acd466a31932b275Christian MaedercspCheckSymbList l = case l of
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder CspSymbMap (CspSymb a Nothing) Nothing
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder : CspSymbMap (CspSymb b (Just t)) _ : r -> mkDiag Warning
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder ("profile '" ++ showDoc t "' does not apply to '"
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder ++ showId a "' but only to") b : cspCheckSymbList r
62eaa2fb831613d8a6e59687f83a45be1041ab17Christian Maeder _ : r -> cspCheckSymbList r
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedertoChanSymbol :: (CHANNEL_NAME, SORT) -> CspSymbol
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedertoChanSymbol (c, s) = CspSymbol c $ ChanAsItemType s
67869d63d1725c79e4c07b51acd466a31932b275Christian MaedertoProcSymbol :: (PROCESS_NAME, ProcProfile) -> CspSymbol
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedertoProcSymbol (n, p) = CspSymbol n $ ProcAsItemType p
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederidToCspRaw :: Id -> CspRawSymbol
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaederidToCspRaw = CspKindedSymb $ CaslKind Implicit
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedersortToProcProfile :: SORT -> ProcProfile
67869d63d1725c79e4c07b51acd466a31932b275Christian MaedersortToProcProfile = ProcProfile [] . Set.singleton . CommTypeSort
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedercspTypedSymbKindToRaw :: Bool -> CspCASLSign -> CspSymbKind -> Id -> CspType
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder -> Result CspRawSymbol
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian MaedercspTypedSymbKindToRaw b sig k idt t = let
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder csig = extendedInfo sig
67869d63d1725c79e4c07b51acd466a31932b275Christian Maeder chs = getSet $ chans csig
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder prs = getSet $ procSet csig
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder reduce = reduceProcProfile $ sortRel sig
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder err = plain_error (idToCspRaw idt)
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder (showDoc idt " " ++ showDoc t
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder " does not have kind " ++ showDoc k "") nullRange
67869d63d1725c79e4c07b51acd466a31932b275Christian Maeder ProcessKind -> case t of
1bc5dccbf0083a620ae1181c717fea75e4af5e5cChristian Maeder ProcType p -> return $ ACspSymbol $ toProcSymbol (idt, reduce p)
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder CaslType (A_type s) -> return
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder $ ACspSymbol $ toProcSymbol
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder (idt, reduce $ sortToProcProfile s)
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder ChannelKind -> case t of
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder CaslType (A_type s) ->
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder return $ ACspSymbol $ toChanSymbol (idt, s)
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder CaslKind ck -> case t of
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder CaslType ct -> let
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder caslAnno = fmap (\ r -> case r of
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder ASymbol sy -> ACspSymbol $ CspSymbol idt $ CaslSymbType $ symbType sy
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder _ -> CspKindedSymb k idt) $ typedSymbKindToRaw b sig ck idt ct
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder in case ct of
65dce48b81f69e11a36bf1051314a845299446e1Christian Maeder A_type s | b && ck == Implicit ->
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder let hasChan = Set.member s chs
65dce48b81f69e11a36bf1051314a845299446e1Christian Maeder cprs = Set.filter (\ (ProcProfile args al) ->
65dce48b81f69e11a36bf1051314a845299446e1Christian Maeder null args && any (\ cs -> case cs of
ecf557c0b4f953106755a239da2c0b168064d3f4Christian Maeder CommTypeSort r -> r == s
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder || Set.member s (subsortsOf r sig)
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder CommTypeChan (TypedChanName c _) ->
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder c == s) (Set.toList al)) prs
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder in case Set.toList cprs of
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder [] -> if hasChan then do
de66af0f4b27f08f81c7ca9c573ef9cdf7ca7a07Christian Maeder appendDiags [mkDiag Hint "matched channel" idt]
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder return $ ACspSymbol $ toChanSymbol (idt, s)
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder else caslAnno
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder pr : rpr -> do
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder when (hasChan || not (null rpr)) $
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder appendDiags [mkDiag Warning "ambiguous matches" idt]
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder appendDiags [mkDiag Hint "matched process" idt]
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder return $ ACspSymbol $ toProcSymbol (idt, pr)
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder _ -> caslAnno
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder ProcType p | ck == Implicit ->
64e1905404e5135e98a26d2ab4150b6764956576Christian Maeder return $ ACspSymbol $ toProcSymbol (idt, reduce p)
-> Result (Map.Map CspRawSymbol CspRawSymbol)
Map.empty (concat ls)
toSymbolSet :: CspSign -> [Set.Set CspSymbol]
toSymbolSet csig = map Set.fromList
symSets :: CspCASLSign -> [Set.Set CspSymbol]
symSets sig = map (Set.map caslToCspSymbol) (symOf sig)
splitSymbolMap :: Map.Map CspRawSymbol CspRawSymbol
-> (RawSymbolMap, Map.Map CspRawSymbol CspRawSymbol)
splitSymbolMap = Map.foldWithKey (\ s t (cm, ccm) ->
(Just c, Just d) -> (Map.insert c d cm, ccm)
getCASLSymbols = Set.fold (\ (CspSymbol i ty) -> case ty of
CaslSymbType t -> Set.insert $ Symbol i t
_ -> id) Set.empty