24.09., 9:00 - 11:00: Due to updates GitLab will be unavailable for some minutes between 09:00 and 11:00.

Property.hs 3.25 KB
Newer Older
Philipp Meyer's avatar
Philipp Meyer committed
1 2 3 4 5 6
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module Property
    (Property(..),
     Formula(..),
     Op(..),
7 8 9 10
     Term(..),
     PropResult(..),
     resultAnd,
     resultOr,
11
     resultNot,
12 13
     resultsAnd,
     resultsOr)
Philipp Meyer's avatar
Philipp Meyer committed
14 15
where

16 17 18 19 20 21 22 23 24 25 26
data Term a =
          Var a
        | Const Integer
        | Minus (Term a)
        | Term a :+: Term a
        | Term a :-: Term a
        | Term a :*: Term a
        deriving (Eq)

instance (Show a) => Show (Term a) where
        show (Var x) = show x
27
        show (Const c) = show c
28 29 30 31
        show (Minus t) = "-" ++ show t
        show (t :+: u) = "(" ++ show t ++ " + " ++ show u ++ ")"
        show (t :-: u) = "(" ++ show t ++ " - " ++ show u ++ ")"
        show (t :*: u) = show t ++ " * " ++ show u
Philipp Meyer's avatar
Philipp Meyer committed
32

33 34 35 36 37 38 39
instance Functor Term where
        fmap f (Var x) = Var (f x)
        fmap _ (Const c) = Const c
        fmap f (Minus t) = Minus (fmap f t)
        fmap f (t :+: u) = fmap f t :+: fmap f u
        fmap f (t :-: u) = fmap f t :-: fmap f u
        fmap f (t :*: u) = fmap f t :*: fmap f u
40

41
data Op = Gt | Ge | Eq | Ne | Le | Lt deriving (Eq)
Philipp Meyer's avatar
Philipp Meyer committed
42 43 44

instance Show Op where
        show Gt = ">"
45
        show Ge = "≥"
Philipp Meyer's avatar
Philipp Meyer committed
46
        show Eq = "="
47
        show Ne = "≠"
48
        show Le = "≤"
Philipp Meyer's avatar
Philipp Meyer committed
49 50
        show Lt = "<"

51 52 53 54 55 56
data Formula a =
          FTrue | FFalse
        | LinearInequation (Term a) Op (Term a)
        | Neg (Formula a)
        | Formula a :&: Formula a
        | Formula a :|: Formula a
57
             deriving (Eq)
Philipp Meyer's avatar
Philipp Meyer committed
58 59 60 61

infixr 3 :&:
infixr 2 :|:

62
instance (Show a) => Show (Formula a) where
63 64
        show FTrue = "true"
        show FFalse = "false"
65 66
        show (LinearInequation lhs op rhs) =
            show lhs ++ " " ++ show op ++ " " ++ show rhs
67
        show (Neg p) = "¬" ++ "(" ++ show p ++ ")"
Philipp Meyer's avatar
Philipp Meyer committed
68 69 70
        show (p :&: q) = "(" ++ show p ++ " ∧ " ++ show q ++ ")"
        show (p :|: q) = "(" ++ show p ++ " ∨ " ++ show q ++ ")"

71 72 73 74 75 76 77 78
instance Functor Formula where
        fmap _ FTrue = FTrue
        fmap _ FFalse = FFalse
        fmap f (LinearInequation lhs op rhs) =
                LinearInequation (fmap f lhs) op (fmap f rhs)
        fmap f (Neg p) = Neg (fmap f p)
        fmap f (p :&: q) = fmap f p :&: fmap f q
        fmap f (p :|: q) = fmap f p :|: fmap f q
79

Philipp Meyer's avatar
Philipp Meyer committed
80 81 82
data Property = Correctness
              | LayeredTermination
              | StrongConsensus
Philipp Meyer's avatar
Philipp Meyer committed
83

Philipp Meyer's avatar
Philipp Meyer committed
84 85 86
instance Show Property where
        show LayeredTermination = "layered termination"
        show StrongConsensus = "strong consensus"
Philipp Meyer's avatar
Philipp Meyer committed
87

Philipp Meyer's avatar
Philipp Meyer committed
88
data PropResult = Satisfied | Unsatisfied | Unknown deriving (Eq)
Philipp Meyer's avatar
Philipp Meyer committed
89

Philipp Meyer's avatar
Philipp Meyer committed
90 91 92 93
instance Show PropResult where
        show Satisfied = "satisfied"
        show Unsatisfied = "not satisfied"
        show Unknown = "may not be satisfied"
94 95 96 97 98 99 100 101 102 103 104 105 106

resultAnd :: PropResult -> PropResult -> PropResult
resultAnd Satisfied x = x
resultAnd Unsatisfied _ = Unsatisfied
resultAnd _ Unsatisfied = Unsatisfied
resultAnd Unknown _ = Unknown

resultOr :: PropResult -> PropResult -> PropResult
resultOr Satisfied _ = Satisfied
resultOr _ Satisfied = Satisfied
resultOr Unsatisfied x = x
resultOr Unknown _ = Unknown

107 108 109 110 111
resultNot :: PropResult -> PropResult
resultNot Satisfied = Unsatisfied
resultNot Unsatisfied = Unsatisfied
resultNot Unknown = Unknown

112 113 114 115 116
resultsAnd :: [PropResult] -> PropResult
resultsAnd = foldr resultAnd Satisfied

resultsOr :: [PropResult] -> PropResult
resultsOr = foldr resultOr Unsatisfied