-- |

-- Module      : OAlg.Data.Logical

-- Description : logical connectives.

-- Copyright   : (c) Erich Gut

-- License     : BSD3

-- Maintainer  : zerich.gut@gmail.com

--

-- Types admitting logical connectives.

module OAlg.Data.Logical
  ( -- * Logical Operators

    Logical(..), Erasable(..)
  )
  where

import Prelude hiding ((||),(&&))
import qualified Data.Bool as P

--------------------------------------------------------------------------------

-- Lagical -


-- | logical structures admitting a general definition for /disjunctions/ and /conjunctions/.

class Logical a where
  infixr 2 ||
  -- | disjunction

  (||) :: a -> a -> a
  
  infixr 3 &&  
  -- | conjunction

  (&&) :: a -> a -> a

instance Logical Bool where
  || :: Bool -> Bool -> Bool
(||)  = Bool -> Bool -> Bool
(P.||)
  && :: Bool -> Bool -> Bool
(&&)  = Bool -> Bool -> Bool
(P.&&)

instance Logical b => Logical (x -> b) where
  x -> b
f || :: (x -> b) -> (x -> b) -> x -> b
|| x -> b
g = \x
x -> x -> b
f x
x b -> b -> b
forall a. Logical a => a -> a -> a
|| x -> b
g x
x
  x -> b
f && :: (x -> b) -> (x -> b) -> x -> b
&& x -> b
g = \x
x -> x -> b
f x
x b -> b -> b
forall a. Logical a => a -> a -> a
&& x -> b
g x
x

--------------------------------------------------------------------------------

-- Erasable -


-- | erasor-operator.

class Erasable a where
  infixl 4 //
  -- | difference

  (//) :: a -> a -> a

instance Erasable Bool where Bool
a // :: Bool -> Bool -> Bool
// Bool
b = Bool
a Bool -> Bool -> Bool
forall a. Logical a => a -> a -> a
&& Bool -> Bool
not Bool
b

instance Eq x => Erasable [x] where
  [x]
xs // :: [x] -> [x] -> [x]
// [] = [x]
xs
  [] // [x]
_  = []
  (x
x:[x]
xs) // (x
y:[x]
ys) = case x
x x -> x -> Bool
forall a. Eq a => a -> a -> Bool
== x
y of
    Bool
True  -> [x]
xs [x] -> [x] -> [x]
forall a. Erasable a => a -> a -> a
// [x]
ys
    Bool
False -> x
x x -> [x] -> [x]
forall a. a -> [a] -> [a]
: ([x]
xs [x] -> [x] -> [x]
forall a. Erasable a => a -> a -> a
// (x
yx -> [x] -> [x]
forall a. a -> [a] -> [a]
:[x]
ys))