module TPDB.Data.Rule where

import TPDB.Data.Identifier
import Data.Typeable

data Relation = Strict |  Weak | Equal deriving ( Relation -> Relation -> Bool
(Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool) -> Eq Relation
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Relation -> Relation -> Bool
== :: Relation -> Relation -> Bool
$c/= :: Relation -> Relation -> Bool
/= :: Relation -> Relation -> Bool
Eq, Eq Relation
Eq Relation =>
(Relation -> Relation -> Ordering)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Bool)
-> (Relation -> Relation -> Relation)
-> (Relation -> Relation -> Relation)
-> Ord Relation
Relation -> Relation -> Bool
Relation -> Relation -> Ordering
Relation -> Relation -> Relation
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Relation -> Relation -> Ordering
compare :: Relation -> Relation -> Ordering
$c< :: Relation -> Relation -> Bool
< :: Relation -> Relation -> Bool
$c<= :: Relation -> Relation -> Bool
<= :: Relation -> Relation -> Bool
$c> :: Relation -> Relation -> Bool
> :: Relation -> Relation -> Bool
$c>= :: Relation -> Relation -> Bool
>= :: Relation -> Relation -> Bool
$cmax :: Relation -> Relation -> Relation
max :: Relation -> Relation -> Relation
$cmin :: Relation -> Relation -> Relation
min :: Relation -> Relation -> Relation
Ord, Typeable, Int -> Relation -> ShowS
[Relation] -> ShowS
Relation -> String
(Int -> Relation -> ShowS)
-> (Relation -> String) -> ([Relation] -> ShowS) -> Show Relation
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Relation -> ShowS
showsPrec :: Int -> Relation -> ShowS
$cshow :: Relation -> String
show :: Relation -> String
$cshowList :: [Relation] -> ShowS
showList :: [Relation] -> ShowS
Show )

data Rule a = Rule
  { forall a. Rule a -> a
lhs :: !a, forall a. Rule a -> a
rhs :: !a 
  , forall a. Rule a -> Relation
relation :: !Relation
  , forall a. Rule a -> Bool
top :: !Bool
  -- TPDB (XTC) represents SRS as TRS,
  -- e.g.,  "ab -> ba" is "a(b(x)) -> b(a(x))",
  -- and when we convert back (as we need for CPF),
  -- need to use the original variable in the rule
  , forall a. Rule a -> Maybe Identifier
original_variable :: !(Maybe Identifier)
  }
    deriving ( Rule a -> Rule a -> Bool
(Rule a -> Rule a -> Bool)
-> (Rule a -> Rule a -> Bool) -> Eq (Rule a)
forall a. Eq a => Rule a -> Rule a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall a. Eq a => Rule a -> Rule a -> Bool
== :: Rule a -> Rule a -> Bool
$c/= :: forall a. Eq a => Rule a -> Rule a -> Bool
/= :: Rule a -> Rule a -> Bool
Eq, Eq (Rule a)
Eq (Rule a) =>
(Rule a -> Rule a -> Ordering)
-> (Rule a -> Rule a -> Bool)
-> (Rule a -> Rule a -> Bool)
-> (Rule a -> Rule a -> Bool)
-> (Rule a -> Rule a -> Bool)
-> (Rule a -> Rule a -> Rule a)
-> (Rule a -> Rule a -> Rule a)
-> Ord (Rule a)
Rule a -> Rule a -> Bool
Rule a -> Rule a -> Ordering
Rule a -> Rule a -> Rule a
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall a. Ord a => Eq (Rule a)
forall a. Ord a => Rule a -> Rule a -> Bool
forall a. Ord a => Rule a -> Rule a -> Ordering
forall a. Ord a => Rule a -> Rule a -> Rule a
$ccompare :: forall a. Ord a => Rule a -> Rule a -> Ordering
compare :: Rule a -> Rule a -> Ordering
$c< :: forall a. Ord a => Rule a -> Rule a -> Bool
< :: Rule a -> Rule a -> Bool
$c<= :: forall a. Ord a => Rule a -> Rule a -> Bool
<= :: Rule a -> Rule a -> Bool
$c> :: forall a. Ord a => Rule a -> Rule a -> Bool
> :: Rule a -> Rule a -> Bool
$c>= :: forall a. Ord a => Rule a -> Rule a -> Bool
>= :: Rule a -> Rule a -> Bool
$cmax :: forall a. Ord a => Rule a -> Rule a -> Rule a
max :: Rule a -> Rule a -> Rule a
$cmin :: forall a. Ord a => Rule a -> Rule a -> Rule a
min :: Rule a -> Rule a -> Rule a
Ord, Typeable )

strict :: Rule a -> Bool
strict :: forall a. Rule a -> Bool
strict Rule a
u = case Rule a -> Relation
forall a. Rule a -> Relation
relation Rule a
u of Relation
Strict -> Bool
True ; Relation
_ -> Bool
False

weak :: Rule a -> Bool
weak :: forall a. Rule a -> Bool
weak Rule a
u = case Rule a -> Relation
forall a. Rule a -> Relation
relation Rule a
u of Relation
Weak -> Bool
True ; Relation
_ -> Bool
False

equal :: Rule a -> Bool
equal :: forall a. Rule a -> Bool
equal Rule a
u = case Rule a -> Relation
forall a. Rule a -> Relation
relation Rule a
u of Relation
Equal -> Bool
True ; Relation
_ -> Bool
False

instance Functor Rule where 
    fmap :: forall a b. (a -> b) -> Rule a -> Rule b
fmap a -> b
f Rule a
u = Rule a
u { lhs = f $ lhs u, rhs = f $ rhs u }