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.04 KB
Newer Older
Philipp Meyer's avatar
Philipp Meyer committed
1 2 3 4
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

module Property
    (Property(..),
5
     showPropertyName,
6
     rename,
Philipp Meyer's avatar
Philipp Meyer committed
7 8 9 10
     PropertyType(..),
     Formula(..),
     LinearInequation(..),
     Op(..),
11
     Term(..))
Philipp Meyer's avatar
Philipp Meyer committed
12 13
where

14 15 16 17 18 19
data Term = Var String
          | Const Integer
          | Minus Term
          | Term :+: Term
          | Term :-: Term
          | Term :*: Term
Philipp Meyer's avatar
Philipp Meyer committed
20 21

instance Show Term where
22 23
        show (Var x) = x
        show (Const c) = show c
24 25 26 27
        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
28

29 30 31 32 33 34 35 36
renameTerm :: (String -> String) -> Term -> Term
renameTerm f (Var x) = Var (f x)
renameTerm _ (Const c) = Const c
renameTerm f (Minus t) = Minus (renameTerm f t)
renameTerm f (t :+: u) = renameTerm f t :+: renameTerm f u
renameTerm f (t :-: u) = renameTerm f t :-: renameTerm f u
renameTerm f (t :*: u) = renameTerm f t :*: renameTerm f u

37
data Op = Gt | Ge | Eq | Ne | Le | Lt
Philipp Meyer's avatar
Philipp Meyer committed
38 39 40

instance Show Op where
        show Gt = ">"
41
        show Ge = "≥"
Philipp Meyer's avatar
Philipp Meyer committed
42
        show Eq = "="
43
        show Ne = "≠"
44
        show Le = "≤"
Philipp Meyer's avatar
Philipp Meyer committed
45 46
        show Lt = "<"

47
-- TODO: merge LinIneq constructor into Formula
Philipp Meyer's avatar
Philipp Meyer committed
48 49 50 51 52
data LinearInequation = LinIneq Term Op Term

instance Show LinearInequation where
        show (LinIneq lhs op rhs) = show lhs ++ " " ++ show op ++ " " ++ show rhs

53 54 55 56
renameLinIneq :: (String -> String) -> LinearInequation -> LinearInequation
renameLinIneq f (LinIneq lhs op rhs) =
        LinIneq (renameTerm f lhs) op (renameTerm f rhs)

57 58
data Formula = FTrue | FFalse
             | Atom LinearInequation
Philipp Meyer's avatar
Philipp Meyer committed
59 60 61 62 63 64 65 66
             | Neg Formula
             | Formula :&: Formula
             | Formula :|: Formula

infixr 3 :&:
infixr 2 :|:

instance Show Formula where
67 68
        show FTrue = "true"
        show FFalse = "false"
Philipp Meyer's avatar
Philipp Meyer committed
69
        show (Atom a) = show a
70
        show (Neg p) = "¬" ++ "(" ++ show p ++ ")"
Philipp Meyer's avatar
Philipp Meyer committed
71 72 73
        show (p :&: q) = "(" ++ show p ++ " ∧ " ++ show q ++ ")"
        show (p :|: q) = "(" ++ show p ++ " ∨ " ++ show q ++ ")"

74 75 76 77 78 79 80 81
renameFormula :: (String -> String) -> Formula -> Formula
renameFormula _ FTrue = FTrue
renameFormula _ FFalse = FFalse
renameFormula f (Atom a) = Atom (renameLinIneq f a)
renameFormula f (Neg p) = Neg (renameFormula f p)
renameFormula f (p :&: q) = renameFormula f p :&: renameFormula f q
renameFormula f (p :|: q) = renameFormula f p :|: renameFormula f q

Philipp Meyer's avatar
Philipp Meyer committed
82 83 84 85 86 87
data PropertyType = Safety | Liveness

instance Show PropertyType where
        show Safety = "safety"
        show Liveness = "liveness"

88 89 90 91 92
data Property = Property {
        pname :: String,
        ptype :: PropertyType,
        pformula :: Formula
}
Philipp Meyer's avatar
Philipp Meyer committed
93 94

instance Show Property where
95
        show p =
96
            showPropertyName p ++ " { " ++ show (pformula p) ++ " }"
Philipp Meyer's avatar
Philipp Meyer committed
97

98 99 100
rename :: (String -> String) -> Property -> Property
rename f (Property pn pt pf) = Property pn pt (renameFormula f pf)

101
showPropertyName :: Property -> String
102
showPropertyName p = show (ptype p) ++ " property" ++
103
               (if null (pname p) then "" else " " ++ show (pname p))