1 {-# LANGUAGE DataKinds #-}
16 import Data.Eq (Eq(..))
17 import Data.Maybe (Maybe(..), isJust)
18 import Data.Proxy (Proxy(..))
20 import Numeric.Natural (Natural)
21 import Text.Show (Show(..))
22 import Unsafe.Coerce (unsafeCoerce)
23 import qualified Data.Ord as Ord
24 import qualified Prelude as Num
26 import Utils.Constraint
28 -- | Convenient wrapper around 'natVal' to be used with @TypeApplications@.
29 nat :: forall (n::Nat). KnownNat n => Natural
30 nat = natVal (Proxy::Proxy n)
33 -- | Convenient alias.
37 -- | Proofs implied by @(i'+'j'<='n)@
38 (+<=) :: (i+j<=n) => Proof (i<=n, j<=n, j<=n-i, i<=n-j)
39 (+<=) = unsafeCoerce (Proof::Proof ((),(),(),()))
41 -- | Proofs implied by @(i'<='j)@.
43 KnownNat i => KnownNat j =>
44 (i<=j) => Proof ( KnownNat (j-i)
48 (<=) | SomeNat (_jmi::Proxy jmi) <- someNatVal (nat @j Num.- nat @i)
49 = unsafeCoerce (Proof::Proof (KnownNat jmi, (), (), ()))
52 (+) :: forall i j. KnownNat i => KnownNat j => Proof (KnownNat (i+j))
53 (+) | SomeNat (_ipj::Proxy ipj) <- someNatVal (nat @i Num.+ nat @j)
54 = unsafeCoerce (Proof::Proof (KnownNat ipj))
57 (-) :: forall i j. (j<=i) => KnownNat i => KnownNat j => Proof (KnownNat (i-j))
58 (-) | SomeNat (_imj::Proxy imj) <- someNatVal (nat @i Num.- nat @j)
59 = unsafeCoerce (Proof::Proof (KnownNat imj))
63 KnownNat i => KnownNat j =>
69 (<=?) | nat @i Ord.<= nat @j
70 , SomeNat (_jmi::Proxy jmi) <- someNatVal (nat @j Num.- nat @i)
71 = Just (unsafeCoerce (Proof::Proof ((),KnownNat jmi,(),(),())))
76 Index :: (KnownNat i, (i+1<=len)) => Proxy i -> Index len
77 instance Eq (Index len) where
78 Index i == Index j = isJust (sameNat i j)
79 instance Show (Index len) where
80 showsPrec p (Index i) = showsPrec p (natVal i)