{-# LANGUAGE TypeFamilies #-}
module Logic.Model
(
KripkeStructure(..)
, State(..)
, Transition(..)
, stateIds
, lookupState
, getState
, transitionIds
, lookupTransition
, getTransition
, nextStates
, precedes
, prevStates
, follows
, Element(..)
, findById
) where
import Data.Maybe
data KripkeStructure a = KripkeStructure
{ states :: [State a]
, transitions :: [Transition a]
}
deriving (Show, Read, Eq)
data State a
= State Int [a]
deriving (Show, Read, Eq)
data Transition a = Transition
{ transitionId :: Int
, source :: Int
, target :: Int
, transitionPayload :: [a]
}
deriving (Show, Read, Eq)
stateIds :: KripkeStructure a -> [Int]
stateIds =
map elementId . states
lookupState :: Int -> KripkeStructure a -> Maybe (State a)
lookupState i =
findById i . states
getState :: Int -> KripkeStructure a -> State a
getState i =
fromJust . lookupState i
transitionIds :: KripkeStructure a -> [Int]
transitionIds =
map elementId . transitions
lookupTransition :: Int -> KripkeStructure a -> Maybe (Transition a)
lookupTransition i =
findById i . transitions
getTransition :: Int -> KripkeStructure a -> Transition a
getTransition i =
fromJust . lookupTransition i
nextStates :: KripkeStructure a -> Int -> [Int]
nextStates (KripkeStructure _ transitions) state =
map target $ filter (\t -> source t == state) transitions
follows :: KripkeStructure a -> Int -> Int -> Bool
follows ts s1 s2 =
s1 `elem` nextStates ts s2
prevStates :: KripkeStructure a -> Int -> [Int]
prevStates (KripkeStructure _ transitions) state =
map source $ filter (\t -> target t == state) transitions
precedes :: KripkeStructure a -> Int -> Int -> Bool
precedes ts s1 s2 =
s1 `elem` prevStates ts s2
class Element e where
type Payload e :: *
elementId :: e -> Int
values :: e -> [Payload e]
instance Element (State a) where
type Payload (State a) = a
elementId (State i _) = i
values (State _ v) = v
instance Element (Transition a) where
type Payload (Transition a) = a
elementId = transitionId
values = transitionPayload
findById :: Element a => Int -> [a] -> Maybe a
findById i elems =
listToMaybe $ filter (\e -> elementId e == i) elems