Powerset.hs 1.83 KB
Newer Older
1
{-# LANGUAGE InstanceSigs #-}
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TupleSections #-}

module Data.Functors.Powerset where

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

data Powerset = Powerset
  deriving (Show)

instance RefinementInterface Powerset where
19
  -- | No edge labels
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
20
  type Label Powerset = ()
21
  -- | Tuple of (|edgesToC\S|, |edgesToS|)
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
22
  type Weight Powerset = (Int, Int)
23
  -- | Does this state have at least one successor?
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
24
  type H1 Powerset = Bool
25
26
27
28
  -- | 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
29
30
  type H3 Powerset = (Bool, Bool, Bool)

31
  parse _ arr = do
32
    successors <- V.imapM parseNode arr
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
33

34
35
    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
36
37
38
39
40
41
42

    return $ Encoding.new structure edges

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

43
44
45
  init :: Powerset -> H1 Powerset -> [Label Powerset] -> Weight Powerset
  init _ _ = (0, ) . length

46
47
48
49
50
51
52
53
54
55
56
57
  update :: Powerset -> [Label Powerset] -> Weight Powerset
         -> (Weight Powerset, H3 Powerset, Weight Powerset)
  update _ labels (toRest, toC) =
    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)

Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
58
59
60
61
62
powerset :: RefinableFunctor
powerset = RefinableFunctor
  { name = "Powerset"
  , parseArguments = NoArguments Powerset
  }