Powerset.hs 1.94 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, 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
20
21
22
-- New interface

data Powerset' a = Powerset' a

-- Old interface

Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
23
24
25
data Powerset = Powerset
  deriving (Show)

26
27
28
powerset :: FunctorParser Powerset
powerset = Prefix (L.symbol "P" >> pure Powerset)

Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
29
instance RefinementInterface Powerset where
30
  -- | No edge labels
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
31
  type Label Powerset = ()
32
  -- | Tuple of (|edgesToC\S|, |edgesToS|)
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
33
  type Weight Powerset = (Int, Int)
34
  -- | Does this state have at least one successor?
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
35
  type H1 Powerset = Bool
36
37
38
39
  -- | 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
40
41
  type H3 Powerset = (Bool, Bool, Bool)

42
  parse _ arr = do
43
    successors <- V.imapM parseNode arr
Hans-Peter Deifel's avatar
Hans-Peter Deifel committed
44

45
46
    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
47
48
49
50
51
52
53

    return $ Encoding.new structure edges

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

54
55
  init :: H1 Powerset -> [Label Powerset] -> Weight Powerset
  init _ = (0, ) . length
56

57
  update :: [Label Powerset] -> Weight Powerset
58
         -> (Weight Powerset, H3 Powerset, Weight Powerset)
59
  update labels (toRest, toC) =
60
61
62
63
64
65
66
67
    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)