Powerset.hs 1.86 KB
Newer Older
1
{-# LANGUAGE InstanceSigs #-}
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
2
3
4
5
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}

6
module Data.Functors.Powerset (Powerset(..), powerset) where
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
7
8
9
10
11
12
13

import           Data.Vector (Vector)
import qualified Data.Vector as V
import qualified Data.Yaml as Yaml

import           Data.RefinementInterface
import qualified Data.MorphismEncoding as Encoding
14
15
import           Text.Parser.Functor
import qualified Text.Parser.Lexer as L
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
16
17
18
19

data Powerset = Powerset
  deriving (Show)

20
21
22
powerset :: FunctorParser Powerset
powerset = Prefix (L.symbol "P" >> pure Powerset)

Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
23
instance RefinementInterface Powerset where
24
  -- | No edge labels
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
25
  type Label Powerset = ()
26
  -- | Tuple of (|edgesToC\S|, |edgesToS|)
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
27
  type Weight Powerset = (Int, Int)
28
  -- | Does this state have at least one successor?
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
29
  type H1 Powerset = Bool
30
31
32
33
  -- | Tuple of:
  --    - do we have edges to the rest?
  --    - do we have edges to C\S?
  --    - do we have edges to S?
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
34
35
  type H3 Powerset = (Bool, Bool, Bool)

36
  parse _ arr = do
37
    successors <- V.imapM parseNode arr
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
38

39
40
    let structure = V.map (not . V.null . snd) successors
        edges = V.concatMap (\(from, succs) -> fmap (Encoding.Edge from ()) succs) successors
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
41
42
43
44
45
46
47

    return $ Encoding.new structure edges

    where
      parseNode :: Int -> Yaml.Value -> Yaml.Parser (Int, Vector Int)
      parseNode nodeIdx value = (nodeIdx,) <$> Yaml.parseJSON value

48
49
  init :: H1 Powerset -> [Label Powerset] -> Weight Powerset
  init _ = (0, ) . length
50

51
  update :: [Label Powerset] -> Weight Powerset
52
         -> (Weight Powerset, H3 Powerset, Weight Powerset)
53
  update labels (toRest, toC) =
54
55
56
57
58
59
60
61
    let
      toS = length labels
      toCwithoutS = toC - toS
      weightToS = (toRest + toCwithoutS, toS)
      h3 = (toRest > 0, toCwithoutS > 0, toS > 0)
      weightToCwithoutS = (toRest + toS, toCwithoutS)
    in
      (weightToS, h3, weightToCwithoutS)