Static analysis for CSP-CASL
basicAnalysisCspCASL :: (CspBasicSpec, CspSign, GlobalAnnos)
-> Result (CspBasicSpec, ExtSign CspSign (), [Named ()])
basicAnalysisCspCASL (cc, sigma, _ga) = do
let (_, accSig) = runState (ana_BASIC_CSP cc) sigma
let ds = reverse $ envDiags accSig
Result ds (Just ()) -- insert diagnostics
return (cc, mkExtSign accSig, [])
ana_BASIC_CSP :: CspBasicSpec -> State CspSign CspBasicSpec
chs <- anaChanDecls (channels cc)
peqs <- anaProcItems (proc_items cc)
return (CspBasicSpec chs peqs)
-- Analysis of local top elements
-- | Check CspCASL signature for local top elements in subsorts.
checkLocalTops :: State CspSign ()
addDiags (map lteError obs)
-- | Add diagnostic error for every unmet local top element obligation.
lteError :: Obligation SORT -> Diagnosis
lteError (Obligation x y z) = mkDiag Error msg ()
where msg = ("local top element obligation ("
++ (show x) ++ "<" ++ (show y) ++ "," ++ (show z)
-- Analysis of channel declarations
anaChanDecls :: [CHANNEL_DECL] -> State CspSign [CHANNEL_DECL]
anaChanDecls cs = mapM (anaChanDecl) cs
anaChanDecl :: CHANNEL_DECL -> State CspSign CHANNEL_DECL
anaChanDecl (ChannelDecl chanNames chanSort) = do
let ext = extendedInfo sig
newChanMap <-
Monad.foldM (anaChannelName chanSort) oldChanMap chanNames
put sig { extendedInfo = ext { chans = newChanMap }
return (ChannelDecl chanNames chanSort)
anaChannelName :: SORT -> ChanNameMap -> CHANNEL_NAME ->
State CspSign ChanNameMap
anaChannelName s m chanName = do
then do let err = "channel name already in use as a sort name"
addDiags [mkDiag Error err chanName]
Nothing -> return (
Map.insert chanName s m) -- insert new.
then do let warn = "channel redeclared with same sort"
addDiags [mkDiag Warning warn chanName]
return m -- already declared with this sort.
else do let err = "channel declared with multiple sorts"
addDiags [mkDiag Error err chanName]
-- Analysis of process items
anaProcItems :: [PROC_ITEM] -> State CspSign [PROC_ITEM]
anaProcItems ps = mapM (anaProcItem) ps
anaProcItem :: PROC_ITEM -> State CspSign PROC_ITEM
(Proc_Decl name argSorts alpha) -> anaProcDecl name argSorts alpha
(Proc_Eq parmProcName procTerm) -> anaProcEq parmProcName procTerm
-- Analysis of process declarations
anaProcDecl :: PROCESS_NAME -> PROC_ARGS -> PROC_ALPHABET
-> State CspSign PROC_ITEM
anaProcDecl name argSorts (ProcAlphabet cts x) = do
let ext = extendedInfo sig
oldProcDecls = procSet ext
newProcDecls <- -- add new declaration to modify
then do -- duplicate process declaration
let err = "process name declared more than once"
addDiags [mkDiag Error err name]
else do -- new process declation
let profile = (ProcProfile argSorts alpha)
put sig { extendedInfo = ext {procSet = newProcDecls }
-- XXX Should be a CommAlpha not a COMM_ALPHA here - pah!
return (Proc_Decl name argSorts (ProcAlphabet cts x))
checkCommType :: CspSign -> CommAlpha -> COMM_TYPE -> State CspSign CommAlpha
checkCommType sig alpha ct =
then do return (
Set.insert (CommTypeSort ctSort) alpha)
else case
Map.lookup ct (chans $ extendedInfo sig) of
Just s -> return (
Set.insert (CommTypeChan (TypedChanName ct s)) alpha)
Nothing -> do let err = "not a sort or channel name"
addDiags [mkDiag Error err ct]
where ctSort = simpleIdToId ct
-- Analysis of process equations
anaProcEq :: PARM_PROCNAME -> PROCESS -> State CspSign PROC_ITEM
anaProcEq (ParmProcname pn vs) proc = do
let ext = extendedInfo sig
-- Only analyse a process if its name (and thus profile) is known
Just pf -> do gVars <- anaProcVars pn (procArgs pf) vs
anaProcess proc (procAlphabet pf) gVars
Map.empty Nothing -> do addDiags [mkDiag Error "unknown process" pn]
put sig { envDiags = vds }
return (Proc_Eq (ParmProcname pn vs) proc)
anaProcVars :: PROCESS_NAME -> [SORT] -> [VAR] -> State CspSign ProcVarMap
anaProcVars pn ss vs = do
case (compare (length ss) (length vs)) of
LT -> do addDiags [mkDiag Error "too many process arguments" pn]
GT -> do addDiags [mkDiag Error "not enough process arguments" pn]
anaProcVar :: ProcVarMap -> (VAR, SORT) -> State CspSign ProcVarMap
anaProcVar old (v, s) = do
then do addDiags [mkDiag Error "process arg declared more than once" v]
-- Analysis of process terms
anaProcess :: PROCESS -> CommAlpha -> ProcVarMap ->
ProcVarMap -> State CspSign CommAlpha
anaProcess proc alpha gVars lVars = do
do addDiags [mkDiag Debug "Skip" proc]
do addDiags [mkDiag Debug "Stop" proc]
do addDiags [mkDiag Debug "Div" proc]
do addDiags [mkDiag Debug "Run" proc]
do addDiags [mkDiag Debug "Chaos" proc]
do addDiags [mkDiag Debug "Prefix" proc]
(evComms, rcvMap) <- anaEvent e (lVars `
Map.union` gVars)
comms <- anaProcess p alpha gVars (rcvMap `
Map.union` lVars)
ExternalPrefixProcess v s p _ ->
do addDiags [mkDiag Debug "External prefix" proc]
comms <- anaProcess p alpha gVars (
Map.insert v s lVars)
InternalPrefixProcess v s p _ ->
do addDiags [mkDiag Debug "Internal prefix" proc]
comms <- anaProcess p alpha gVars (
Map.insert v s lVars)
do addDiags [mkDiag Debug "Sequential" proc]
pComms <- anaProcess p alpha gVars lVars
do addDiags [mkDiag Debug "ExternalChoice" proc]
pComms <- anaProcess p alpha gVars lVars
qComms <- anaProcess q alpha gVars lVars
do addDiags [mkDiag Debug "InternalChoice" proc]
pComms <- anaProcess p alpha gVars lVars
qComms <- anaProcess q alpha gVars lVars
do addDiags [mkDiag Debug "Interleaving" proc]
pComms <- anaProcess p alpha gVars lVars
qComms <- anaProcess q alpha gVars lVars
SynchronousParallel p q _ ->
do addDiags [mkDiag Debug "Synchronous" proc]
pComms <- anaProcess p alpha gVars lVars
qComms <- anaProcess q alpha gVars lVars
GeneralisedParallel p es q _ ->
do addDiags [mkDiag Debug "Generalised parallel" proc]
pComms <- anaProcess p alpha gVars lVars
synComms <- anaEventSet es
qComms <- anaProcess q alpha gVars lVars
AlphabetisedParallel p esp esq q _ ->
do addDiags [mkDiag Debug "Alphabetised parallel" proc]
pComms <- anaProcess p alpha gVars lVars
pSynComms <- anaEventSet esp
qSynComms <- anaEventSet esq
qComms <- anaProcess q alpha gVars lVars
checkCommAlphaSub pSynComms pComms
checkCommAlphaSub qSynComms qComms
do addDiags [mkDiag Debug "Hiding" proc]
pComms <- anaProcess p alpha gVars lVars
hidComms <- anaEventSet es
RelationalRenaming p _ _ ->
do addDiags [mkDiag Debug "Renaming" proc]
anaProcess p alpha gVars lVars
ConditionalProcess _ p q _ ->
do addDiags [mkDiag Debug "Conditional" proc]
pComms <- anaProcess p alpha gVars lVars
qComms <- anaProcess q alpha gVars lVars
let fComms =
Set.empty -- XXX get formula sorts here
do addDiags [mkDiag Debug "Named process" proc]
-- This next works but has useless error messages: no position
-- information, no fine-grained help as to what's causing the error.
checkCommAlphaSub :: CommAlpha -> CommAlpha -> State CspSign ()
checkCommAlphaSub sub super = do
let err = "comm alphabet subset failure"
(closeCspCommAlpha sig super))
else do addDiags [mkDiag Error err ()]
anaEventSet :: EVENT_SET -> State CspSign CommAlpha
anaEventSet (EventSet es _) = do
put sig { envDiags = vds }
anaEvent :: EVENT -> ProcVarMap -> State CspSign (CommAlpha, ProcVarMap)
anaEvent e vars = case e of
Event t _ -> anaTermEvent t vars
Send c t _ -> anaSendEvent c t vars
NonDetSend c v s _ -> anaBindingEvent c v s
Receive c v s _ -> anaBindingEvent c v s
anaTermEvent :: (TERM ()) -> ProcVarMap ->
State CspSign (CommAlpha, ProcVarMap)
-- XXX Need to implement computing a term's sort
anaSendEvent :: CHANNEL_NAME -> (TERM ()) -> ProcVarMap ->
State CspSign (CommAlpha, ProcVarMap)
let ext = extendedInfo sig
addDiags [mkDiag Error "unknown channel" c]
Just _ -> do -- XXX chanSort
-- XXX Need to implement computing a term's sort
-- XXX Need to check sort(t) <= chanSort
anaBindingEvent :: CHANNEL_NAME -> VAR -> SORT ->
State CspSign (CommAlpha, ProcVarMap)
anaBindingEvent c v s = do
let ext = extendedInfo sig
addDiags [mkDiag Error "unknown channel" c]
then do let alpha = [CommTypeSort s
,CommTypeChan (TypedChanName c s)]
else do let err = "sort not a subsort of channel's sort"
addDiags [mkDiag Error err s]