{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeInType #-}
{-# LANGUAGE TypeOperators #-}
module Data.Type.Predicate.Logic (
Evident, Impossible
, type Not, decideNot
, type (&&&), decideAnd
, type (|||), decideOr, type (^||), type (||^)
, type (^^^), decideXor
, type (==>), proveImplies, Implies
, type (<==>), Equiv
, compImpl, explosion, atom
, complementation, doubleNegation, tripleNegation, negateTwice
, contrapositive, contrapositive'
, projAndFst, projAndSnd, injOrLeft, injOrRight
) where
import Data.Singletons
import Data.Singletons.Decide
import Data.Type.Predicate
import Data.Void
data (&&&) :: Predicate k -> Predicate k -> Predicate k
type instance Apply (p &&& q) a = (p @@ a, q @@ a)
infixr 3 &&&
instance (Decidable p, Decidable q) => Decidable (p &&& q) where
decide :: Decide (p &&& q)
decide (Sing a
x :: Sing a) = forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p &&& q) @@ a)
decideAnd @p @q @a (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @p Sing a
x) (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @q Sing a
x)
instance (Provable p, Provable q) => Provable (p &&& q) where
prove :: Prove (p &&& q)
prove Sing a
x = (forall {k1} (p :: k1 ~> *). Provable p => Prove p
prove @p Sing a
x, forall {k1} (p :: k1 ~> *). Provable p => Prove p
prove @q Sing a
x)
decideAnd
:: forall p q a. ()
=> Decision (p @@ a)
-> Decision (q @@ a)
-> Decision ((p &&& q) @@ a)
decideAnd :: forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p &&& q) @@ a)
decideAnd = \case
Proved p @@ a
p -> forall a b. (a -> b) -> (b -> a) -> Decision a -> Decision b
mapDecision (p @@ a
p,) forall a b. (a, b) -> b
snd
Disproved Refuted (p @@ a)
v -> \Decision (q @@ a)
_ -> forall a. Refuted a -> Decision a
Disproved forall a b. (a -> b) -> a -> b
$ \(p @@ a
p, q @@ a
_) -> Refuted (p @@ a)
v p @@ a
p
data (|||) :: Predicate k -> Predicate k -> Predicate k
type instance Apply (p ||| q) a = Either (p @@ a) (q @@ a)
infixr 2 |||
instance (Decidable p, Decidable q) => Decidable (p ||| q) where
decide :: Decide (p ||| q)
decide (Sing a
x :: Sing a) = forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ||| q) @@ a)
decideOr @p @q @a (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @p Sing a
x) (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @q Sing a
x)
decideOr
:: forall p q a. ()
=> Decision (p @@ a)
-> Decision (q @@ a)
-> Decision ((p ||| q) @@ a)
decideOr :: forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ||| q) @@ a)
decideOr = \case
Proved p @@ a
p -> \Decision (q @@ a)
_ -> forall a. a -> Decision a
Proved forall a b. (a -> b) -> a -> b
$ forall a b. a -> Either a b
Left p @@ a
p
Disproved Refuted (p @@ a)
v -> forall a b. (a -> b) -> (b -> a) -> Decision a -> Decision b
mapDecision forall a b. b -> Either a b
Right (forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a. Void -> a
absurd forall b c a. (b -> c) -> (a -> b) -> a -> c
. Refuted (p @@ a)
v) forall a. a -> a
id)
type p ^|| q = p ||| Not p &&& q
type p ||^ q = p &&& Not q ||| q
type p ^^^ q = (p &&& Not q) ||| (Not p &&& q)
decideXor
:: forall p q a. ()
=> Decision (p @@ a)
-> Decision (q @@ a)
-> Decision ((p ^^^ q) @@ a)
decideXor :: forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ^^^ q) @@ a)
decideXor Decision (p @@ a)
p Decision (q @@ a)
q = forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p ||| q) @@ a)
decideOr @(p &&& Not q) @(Not p &&& q) @a
(forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p &&& q) @@ a)
decideAnd @p @(Not q) @a Decision (p @@ a)
p (forall {k1} (p :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (Not p @@ a)
decideNot @q @a Decision (q @@ a)
q))
(forall {k1} (p :: k1 ~> *) (q :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (q @@ a) -> Decision ((p &&& q) @@ a)
decideAnd @(Not p) @q @a (forall {k1} (p :: k1 ~> *) (a :: k1).
Decision (p @@ a) -> Decision (Not p @@ a)
decideNot @p @a Decision (p @@ a)
p) Decision (q @@ a)
q)
data (==>) :: Predicate k -> Predicate k -> Predicate k
type instance Apply (p ==> q) a = p @@ a -> q @@ a
infixr 1 ==>
instance Decidable (Impossible ==> p) where
instance Provable (Impossible ==> p) where
prove :: Prove (Impossible ==> p)
prove = forall {k1} (p :: k1 ~> *). Impossible --> p
explosion @p
instance (Decidable (p ==> q), Decidable q) => Decidable (Not q ==> Not p) where
decide :: Decide (Not q ==> Not p)
decide Sing a
x = case forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @(p ==> q) Sing a
x of
Proved (p ==> q) @@ a
pq -> forall a. a -> Decision a
Proved forall a b. (a -> b) -> a -> b
$ \Apply q a -> Void
vq Apply p a
p -> Apply q a -> Void
vq ((p ==> q) @@ a
pq Apply p a
p)
Disproved Refuted ((p ==> q) @@ a)
vpq -> case forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @q Sing a
x of
Proved Apply q a
q -> forall a. Refuted a -> Decision a
Disproved forall a b. (a -> b) -> a -> b
$ \(Not q ==> Not p) @@ a
_ -> Refuted ((p ==> q) @@ a)
vpq (forall a b. a -> b -> a
const Apply q a
q)
Disproved Apply q a -> Void
vq -> forall a. Refuted a -> Decision a
Disproved forall a b. (a -> b) -> a -> b
$ \(Not q ==> Not p) @@ a
vnpnq -> Refuted ((p ==> q) @@ a)
vpq (forall a. Void -> a
absurd forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Not q ==> Not p) @@ a
vnpnq Apply q a -> Void
vq)
instance Provable (p ==> q) => Provable (Not q ==> Not p) where
prove :: Prove (Not q ==> Not p)
prove = forall {k} (p :: k ~> *) (q :: k ~> *).
(p --> q) -> Not q --> Not p
contrapositive @p @q (forall {k1} (p :: k1 ~> *). Provable p => Prove p
prove @(p ==> q))
instance {-# OVERLAPPING #-} Decidable (p &&& q ==> p) where
instance {-# OVERLAPPING #-} Provable (p &&& q ==> p) where
prove :: Prove ((p &&& q) ==> p)
prove = forall {k1} (p :: Predicate k1) (q :: Predicate k1).
(p &&& q) --> p
projAndFst @p @q
instance {-# OVERLAPPING #-} Decidable (p &&& q ==> q) where
instance {-# OVERLAPPING #-} Provable (p &&& q ==> q) where
prove :: Prove ((p &&& q) ==> q)
prove = forall {k1} (p :: Predicate k1) (q :: Predicate k1).
(p &&& q) --> q
projAndSnd @p @q
instance {-# OVERLAPPING #-} Decidable (p &&& p ==> p) where
instance {-# OVERLAPPING #-} Provable (p &&& p ==> p) where
prove :: Prove ((p &&& p) ==> p)
prove = forall {k1} (p :: Predicate k1) (q :: Predicate k1).
(p &&& q) --> p
projAndFst @p @p
instance {-# OVERLAPPING #-} Decidable (p ==> p ||| q)
instance {-# OVERLAPPING #-} Provable (p ==> p ||| q) where
prove :: Prove (p ==> (p ||| q))
prove = forall {k} (p :: k ~> *) (q :: k ~> *). p --> (p ||| q)
injOrLeft @p @q
instance {-# OVERLAPPING #-} Decidable (q ==> p ||| q)
instance {-# OVERLAPPING #-} Provable (q ==> p ||| q) where
prove :: Prove (q ==> (p ||| q))
prove = forall {k} (p :: Predicate k) (q :: Predicate k). q --> (p ||| q)
injOrRight @p @q
instance {-# OVERLAPPING #-} Decidable (p ==> p ||| p)
instance {-# OVERLAPPING #-} Provable (p ==> p ||| p) where
prove :: Prove (p ==> (p ||| p))
prove = forall {k} (p :: k ~> *) (q :: k ~> *). p --> (p ||| q)
injOrLeft @p @p
type Implies p q = Provable (p ==> q)
type Equiv p q = Provable (p <==> q)
proveImplies :: Prove q -> Prove (p ==> q)
proveImplies :: forall {k1} (q :: k1 ~> *) (p :: k1 ~> *).
Prove q -> Prove (p ==> q)
proveImplies Prove q
q Sing a
x Apply p a
_ = Prove q
q Sing a
x
type (p <==> q) = p ==> q &&& q ==> p
infixr 1 <==>
explosion :: Impossible --> p
explosion :: forall {k1} (p :: k1 ~> *). Impossible --> p
explosion Sing a
x Impossible @@ a
v = forall a. Void -> a
absurd forall a b. (a -> b) -> a -> b
$ Impossible @@ a
v Sing a
x
atom :: p --> Evident
atom :: forall {k1} (p :: k1 ~> *). p --> Evident
atom = forall a b. a -> b -> a
const
complementation :: forall p. (p &&& Not p) --> Impossible
complementation :: forall {k1} (p :: Predicate k1). (p &&& Not p) --> Impossible
complementation Sing a
_ (Apply p a
p, Apply p a -> Void
notP) Sing a
_ = Apply p a -> Void
notP Apply p a
p
instance {-# OVERLAPPING #-} Provable (p &&& Not p ==> Impossible) where
prove :: Prove ((p &&& Not p) ==> Impossible)
prove = forall {k1} (p :: Predicate k1). (p &&& Not p) --> Impossible
complementation @p
contrapositive
:: (p --> q)
-> (Not q --> Not p)
contrapositive :: forall {k} (p :: k ~> *) (q :: k ~> *).
(p --> q) -> Not q --> Not p
contrapositive p --> q
f Sing a
x Not q @@ a
vQ Apply p a
p = Not q @@ a
vQ (p --> q
f Sing a
x Apply p a
p)
contrapositive'
:: forall p q. Decidable q
=> (Not q --> Not p)
-> (p --> q)
contrapositive' :: forall {k1} (p :: Predicate k1) (q :: Predicate k1).
Decidable q =>
(Not q --> Not p) -> p --> q
contrapositive' Not q --> Not p
f Sing a
x p @@ a
p = forall a. Decision a -> Refuted (Refuted a) -> a
elimDisproof (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @q Sing a
x) forall a b. (a -> b) -> a -> b
$ \Refuted (q @@ a)
vQ ->
Not q --> Not p
f Sing a
x Refuted (q @@ a)
vQ p @@ a
p
doubleNegation :: forall p. Decidable p => Not (Not p) --> p
doubleNegation :: forall {k1} (p :: k1 ~> *). Decidable p => Not (Not p) --> p
doubleNegation Sing a
x Not (Not p) @@ a
vvP = forall a. Decision a -> Refuted (Refuted a) -> a
elimDisproof (forall {k1} (p :: k1 ~> *). Decidable p => Decide p
decide @p Sing a
x) forall a b. (a -> b) -> a -> b
$ \Refuted (p @@ a)
vP ->
Not (Not p) @@ a
vvP Refuted (p @@ a)
vP
tripleNegation :: forall p. Not (Not (Not p)) --> Not p
tripleNegation :: forall {k} (p :: Predicate k). Not (Not (Not p)) --> Not p
tripleNegation Sing a
_ Not (Not (Not p)) @@ a
vvvP Apply p a
p = Not (Not (Not p)) @@ a
vvvP forall a b. (a -> b) -> a -> b
$ \Apply p a -> Void
vP -> Apply p a -> Void
vP Apply p a
p
negateTwice :: p --> Not (Not p)
negateTwice :: forall {k} (p :: k ~> *). p --> Not (Not p)
negateTwice Sing a
_ p @@ a
p (p @@ a) -> Void
vP = (p @@ a) -> Void
vP p @@ a
p
projAndFst :: (p &&& q) --> p
projAndFst :: forall {k1} (p :: Predicate k1) (q :: Predicate k1).
(p &&& q) --> p
projAndFst Sing a
_ = forall a b. (a, b) -> a
fst
projAndSnd :: (p &&& q) --> q
projAndSnd :: forall {k1} (p :: Predicate k1) (q :: Predicate k1).
(p &&& q) --> q
projAndSnd Sing a
_ = forall a b. (a, b) -> b
snd
injOrLeft :: forall p q. p --> (p ||| q)
injOrLeft :: forall {k} (p :: k ~> *) (q :: k ~> *). p --> (p ||| q)
injOrLeft Sing a
_ = forall a b. a -> Either a b
Left
injOrRight :: forall p q. q --> (p ||| q)
injOrRight :: forall {k} (p :: Predicate k) (q :: Predicate k). q --> (p ||| q)
injOrRight Sing a
_ = forall a b. b -> Either a b
Right