StatAnaCSP.hs revision e92e93922166c81167de83cc7400403c5d9bb26c
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuDescription : Static analysis of CspCASL
734257b9ea9fcaa18d4e3627f54f5295a99aa1f7Felix Gabriel ManceCopyright : (c) Andy Gimblett, Liam O'Reilly Markus Roggenbach,
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae Bungiu Swansea University 2008
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuLicense : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuMaintainer : a.m.gimblett@swansea.ac.uk
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuStability : provisional
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuPortability : portable
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae BungiuStatic analysis of CSP-CASL specifications, following "Tool Support
15d62726781e67fe6458fbcf0a8c46832a7bb8daFelix Gabriel Mancefor CSP-CASL", MPhil thesis by Andy Gimblett, 2008.
734257b9ea9fcaa18d4e3627f54f5295a99aa1f7Felix Gabriel Mance<http://www.cs.swan.ac.uk/~csandy/mphil/>
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae Bungiuimport qualified Control.Monad as Monad
ed1b8e97e72b2e3e92edaf2eb22a4b5373d705f1Felix Gabriel Manceimport qualified Data.Map as Map
5180a08007989fd364622fc9bc01f82141643f7bFelix Gabriel Manceimport qualified Data.Maybe as Maybe
ccb84c1cb43fb0ed70c5bf2d364e671820473ed5Francisc Nicolae Bungiuimport qualified Data.Set as S
1a38107941725211e7c3f051f7a8f5e12199f03acmaederimport CASL.AS_Basic_CASL (FORMULA(..), OpKind(..), SORT, TERM(..), VAR,
734257b9ea9fcaa18d4e3627f54f5295a99aa1f7Felix Gabriel Manceimport CASL.MixfixParser (emptyMix, Mix(..), makeRules, mkIdSets,
c038fcf2030a6cfac7a261dee48a9eb29edb78eaFelix Gabriel Mance resolveFormula, resolveMixfix, unite)
0be7a9c012366ada63d587898a15c551b499b76dFelix Gabriel Manceimport CASL.Overload (minExpFORMULA, oneExpTerm)
3b15ba1ffa9a23ca14f3882d1390abddfc494009Felix Gabriel Manceimport CASL.StaticAna (allOpIds, allPredIds)
0be7a9c012366ada63d587898a15c551b499b76dFelix Gabriel Manceimport qualified Common.Lib.Rel as Rel
1a38107941725211e7c3f051f7a8f5e12199f03acmaederimport Common.Id (getRange, Id, nullRange, simpleIdToId)
1a38107941725211e7c3f051f7a8f5e12199f03acmaederimport CspCASL.LocalTop (Obligation(..), unmetObs)
1a38107941725211e7c3f051f7a8f5e12199f03acmaederimport CspCASL.Morphism(makeChannelNameSymbol, makeProcNameSymbol)
12078a24d49ba36b83cda9d07c8e8a480c493fe8Felix Gabriel Mance-- | The first element of the returned pair (CspBasicSpec) is the same
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder-- as the inputted version just with some very minor optimisations -
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance-- none in our case, but for CASL - brackets are otimized. This all
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance-- that happens, the mixfixed terms are still mixed fixed terms in
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder-- the returned version.
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel MancebasicAnalysisCspCASL :: (CspBasicSpec, CspCASLSign, GlobalAnnos)
3e0eb79b52a3078a12531efc3f66d0d94fd9938dFelix Gabriel Mance -> Result (CspBasicSpec, ExtSign CspCASLSign Symbol,
3e0eb79b52a3078a12531efc3f66d0d94fd9938dFelix Gabriel Mance [Named CspCASLSen])
9c3f6477a95da46a907326206673b4a5c2164164Felix Gabriel MancebasicAnalysisCspCASL (cc, sigma, ga) =
3e0eb79b52a3078a12531efc3f66d0d94fd9938dFelix Gabriel Mance let Result es mga = mergeGlobalAnnos ga $ globAnnos sigma
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder (_, accSig) = runState (ana_BASIC_CSP cc) $ case mga of
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance Nothing -> sigma
3e0eb79b52a3078a12531efc3f66d0d94fd9938dFelix Gabriel Mance Just nga -> sigma { globAnnos = nga }
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder ds = reverse $ envDiags accSig
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance -- Extract process equations only.
9c3f6477a95da46a907326206673b4a5c2164164Felix Gabriel Mance ext = extendedInfo accSig
316ef492799cd45fea0f5c26932f49adddfda3f7Felix Gabriel Mance ccsents = reverse $ ccSentences ext
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder -- Clean signature here
31a5ba51cd6d24e28a23abf64ce4043a45eabbefFelix Gabriel Mance cleanSig = accSig
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance { extendedInfo = ext { ccSentences = []}}
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder in Result (es ++ ds) $
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder Just (cc, mkExtSign cleanSig, ccsents)
316ef492799cd45fea0f5c26932f49adddfda3f7Felix Gabriel Manceana_BASIC_CSP :: CspBasicSpec -> State CspCASLSign ()
1a38107941725211e7c3f051f7a8f5e12199f03acmaederana_BASIC_CSP cc = do checkLocalTops
31a5ba51cd6d24e28a23abf64ce4043a45eabbefFelix Gabriel Mance mapM anaChanDecl (channels cc)
0be7a9c012366ada63d587898a15c551b499b76dFelix Gabriel Mance mapM anaProcItem (proc_items cc)
e5e3f128bbd44dd393e1038718038bd323f5e415Felix Gabriel Mance-- Analysis of local top elements
e5e3f128bbd44dd393e1038718038bd323f5e415Felix Gabriel Mance-- | Check a CspCASL signature for local top elements in its subsort
1a38107941725211e7c3f051f7a8f5e12199f03acmaeder-- relation.
e5e3f128bbd44dd393e1038718038bd323f5e415Felix Gabriel MancecheckLocalTops :: State CspCASLSign ()
e5e3f128bbd44dd393e1038718038bd323f5e415Felix Gabriel MancecheckLocalTops = do
31a5ba51cd6d24e28a23abf64ce4043a45eabbefFelix Gabriel Mance let obs = unmetObs $ Rel.toList $ Rel.transClosure $ sortRel sig
3e0eb79b52a3078a12531efc3f66d0d94fd9938dFelix Gabriel Mance addDiags (map lteError obs)
15d62726781e67fe6458fbcf0a8c46832a7bb8daFelix Gabriel Mance-- | Add diagnostic error message for every unmet local top element
1a38107941725211e7c3f051f7a8f5e12199f03acmaederlteError :: Obligation SORT -> Diagnosis
15d62726781e67fe6458fbcf0a8c46832a7bb8daFelix Gabriel MancelteError (Obligation x y z) = mkDiag Error msg ()
e5ea4eeaeefd3521ae3475719e18c96cf91637d5Felix Gabriel Mance where msg = ("local top element obligation ("
e5ea4eeaeefd3521ae3475719e18c96cf91637d5Felix Gabriel Mance ++ (show x) ++ "<" ++ (show y) ++ "," ++ (show z)
dda7065c0c0f383558d7d4e8072969c8c41a8ed7Francisc Nicolae Bungiu ++ ") unfulfilled")
1f9274bb2aa44ea236327814dce99946be52e348Felix Gabriel Mance-- Static analysis of channel declarations
30e9cf458094e5970bc06be667558961c2eccff4Felix Gabriel Mance-- | Statically analyse a CspCASL channel declaration.
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel ManceanaChanDecl :: CHANNEL_DECL -> State CspCASLSign ()
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel ManceanaChanDecl (ChannelDecl chanNames chanSort) = do
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance checkSorts [chanSort] -- check channel sort is known
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance let ext = extendedInfo sig
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance oldChanMap = chans ext
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance newChanMap <- Monad.foldM (anaChannelName chanSort) oldChanMap chanNames
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance vds <- gets envDiags
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder put sig { extendedInfo = ext { chans = newChanMap }
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance , envDiags = vds
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance-- | Statically analyse a CspCASL channel name.
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel ManceanaChannelName :: SORT -> ChanNameMap -> CHANNEL_NAME ->
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance State CspCASLSign ChanNameMap
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel ManceanaChannelName s m chanName = do
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance if (show chanName) `S.member` (S.map show (sortSet sig))
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder then do let err = "channel name already in use as a sort name"
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance addDiags [mkDiag Error err chanName]
02c522d7af110fcad567e3db59f444185ad2c22eFelix Gabriel Mance else case Map.lookup chanName m of
12078a24d49ba36b83cda9d07c8e8a480c493fe8Felix Gabriel Mance -- Add the channel name as a symbol to the list of
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder -- newly defined symbols - which is stored in the CASL
return (Map.insert chanName s m) -- insert new.
if name `Map.member` oldProcDecls
return (Map.insert name profile oldProcDecls)
prof = pn `Map.lookup` procDecls
EQ -> Monad.foldM anaProcVar [] (zip vs ss)
(al, fqArgs) <- anaNamedProc proc name args (lVars `Map.union` gVars)
let fqProc = FQProcess (Skip r) S.empty r
return (S.empty, fqProc)
let fqProc = FQProcess (Stop r) S.empty r
return (S.empty, fqProc)
let fqProc = FQProcess (Div r) S.empty r
return (S.empty, fqProc)
(evComms, rcvMap, fqEvent) <- anaEvent e (lVars `Map.union` gVars)
(comms, pFQTerm) <- anaProcTerm p gVars (rcvMap `Map.union` lVars)
let newAlpha = comms `S.union` evComms
(qComms, qFQTerm) <- anaProcTerm q gVars Map.empty
let newAlpha = pComms `S.union` qComms
let newAlpha = pComms `S.union` qComms
let newAlpha = pComms `S.union` qComms
let newAlpha = pComms `S.union` qComms
let newAlpha = pComms `S.union` qComms
let newAlpha = S.unions [pComms, qComms, synComms]
let newAlpha = pComms `S.union` qComms
return (pComms `S.union` hidComms, FQProcess (Hiding pFQTerm fqEs r)
(pComms `S.union` hidComms) r)
let newAlpha = pComms `S.union` renAlpha
(pComms `S.union` renAlpha) r
mfs <- anaFormulaCspCASL (gVars `Map.union` lVars) f
Nothing -> S.empty
let newAlpha = S.unions [pComms, qComms, fComms]
error "CspCASL.StatAnaCSP.anaProcTerm: Unexpected FQProcess"
prof = pn `Map.lookup` procDecls
return (S.empty, terms)
return (S.empty, terms)
(comms, fqEsElems) <- Monad.foldM (anaCommType sig)
(S.empty, []) es
error "CspCASL.StatAnaCSP.anaEventSet: Unexpected FQEventSet"
if ctSort `S.member` (sortSet sig)
do let newAlpha = S.insert (CommTypeSort ctSort) alpha
case Map.lookup ct (chans $ extendedInfo sig) of
let newAlpha = S.insert (mkTypedChan ct s) alpha
-- annChanSend failed - i.e. we forget
error "CspCASL.StatAnaCSP.anaEvent: Unexpected FQEvent"
let alpha = S.singleton $ CommTypeSort s
let binding = Map.singleton v s
case c `Map.lookup` (chans ext) of
case c `Map.lookup` (chans ext) of
-- communicate in i.e. the sort of the variable.
where subsorts = Rel.predecessors (sortRel sig)
error "CspCASL.StatAnaCSP.anaRenaming: Unexpected FQRenaming"
if (not $ S.null totOps)
then return (inAl `S.union` totOps, fqRenamingTerms)
if not (S.null parOps)
then return (inAl `S.union` parOps, fqRenamingTerms)
if not (S.null preds)
then return (inAl `S.union` preds, fqRenamingTerms)
getUnaryOpsById :: Id -> OpKind -> State CspCASLSign (S.Set CommType)
binOpsKind = S.filter (isBin kind) opsWithId
getBinPredsById :: Id -> State CspCASLSign (S.Set CommType)
binPreds = S.filter isBin predsWithId
let extras = ((closeCspCommAlpha sig sub) `S.difference`
if S.null extras
context ++ "): " ++ (show $ S.toList extras))
let newVars = Map.union pm (varMap sig)
if Rel.member termSort cSort (sortRel sig)
else if Rel.member cSort termSort (sortRel sig)
let newVars = Map.union pm (varMap sig)
Conjunction fs _ -> S.unions (map formulaComms fs)
Disjunction fs _ -> S.unions (map formulaComms fs)
Implication f1 f2 _ _ -> (formulaComms f1) `S.union` (formulaComms f2)
Equivalence f1 f2 _ -> (formulaComms f1) `S.union` (formulaComms f2)
Definedness t _ -> S.singleton (CommTypeSort (sortOfTerm t))
Existl_equation t1 t2 _ -> S.fromList [CommTypeSort (sortOfTerm t1),
Strong_equation t1 t2 _ -> S.fromList [CommTypeSort (sortOfTerm t1),
Membership t s _ -> S.fromList [CommTypeSort (sortOfTerm t),
Mixfix_formula t -> S.singleton (CommTypeSort (sortOfTerm t))
_ -> S.empty