{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE EmptyCase #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.Type.Nat.LE (
LE (..),
LEProof (..),
withLEProof,
decideLE,
leZero,
leSucc,
leRefl,
leStep,
leAsym,
leTrans,
leSwap,
leSwap',
leStepL,
lePred,
proofZeroLEZero,
) where
import Data.Type.Dec (Dec (..), Decidable (..), Neg)
import Data.Type.Equality ((:~:) (..))
import Data.Typeable (Typeable)
import Data.Void (absurd)
import Data.Type.Nat
data LEProof n m where
LEZero :: LEProof 'Z m
LESucc :: LEProof n m -> LEProof ('S n) ('S m)
deriving (Typeable)
deriving instance Show (LEProof n m)
instance Eq (LEProof n m) where
_ == _ = True
instance Ord (LEProof n m) where
compare _ _ = EQ
class LE n m where
leProof :: LEProof n m
instance LE 'Z m where
leProof = LEZero
instance (m ~ 'S m', LE n m') => LE ('S n) m where
leProof = LESucc leProof
withLEProof :: LEProof n m -> (LE n m => r) -> r
withLEProof LEZero k = k
withLEProof (LESucc p) k = withLEProof p k
leZero :: LEProof 'Z n
leZero = LEZero
leSucc :: LEProof n m -> LEProof ('S n) ('S m)
leSucc = LESucc
lePred :: LEProof ('S n) ('S m) -> LEProof n m
lePred (LESucc p) = p
leRefl :: forall n. SNatI n => LEProof n n
leRefl = case snat :: SNat n of
SZ -> LEZero
SS -> LESucc leRefl
leStep :: LEProof n m -> LEProof n ('S m)
leStep LEZero = LEZero
leStep (LESucc p) = LESucc (leStep p)
leStepL :: LEProof ('S n) m -> LEProof n m
leStepL (LESucc p) = leStep p
leAsym :: LEProof n m -> LEProof m n -> n :~: m
leAsym LEZero LEZero = Refl
leAsym (LESucc p) (LESucc q) = case leAsym p q of Refl -> Refl
leTrans :: LEProof n m -> LEProof m p -> LEProof n p
leTrans LEZero _ = LEZero
leTrans (LESucc p) (LESucc q) = LESucc (leTrans p q)
leSwap :: forall n m. (SNatI n, SNatI m) => Neg (LEProof n m) -> LEProof ('S m) n
leSwap np = case (snat :: SNat m, snat :: SNat n) of
(_, SZ) -> absurd (np LEZero)
(SZ, SS) -> LESucc LEZero
(SS, SS) -> LESucc $ leSwap $ \p -> np $ LESucc p
leSwap' :: LEProof n m -> LEProof ('S m) n -> void
leSwap' p (LESucc q) = case p of LESucc p' -> leSwap' p' q
decideLE :: forall n m. (SNatI n, SNatI m) => Dec (LEProof n m)
decideLE = case snat :: SNat n of
SZ -> Yes leZero
SS -> caseSnm
where
caseSnm :: forall n' m'. (SNatI n', SNatI m') => Dec (LEProof ('S n') m')
caseSnm = case snat :: SNat m' of
SZ -> No $ \p -> case p of {}
SS -> case decideLE of
Yes p -> Yes (leSucc p)
No p -> No $ \p' -> p (lePred p')
instance (SNatI n, SNatI m) => Decidable (LEProof n m) where
decide = decideLE
proofZeroLEZero :: LEProof n 'Z -> n :~: 'Z
proofZeroLEZero LEZero = Refl