2020-08-19 00:26:56 +03:00
|
|
|
||| Implementation of ordering relations for `Nat`ural numbers
|
|
|
|
module Data.Nat.Order
|
|
|
|
|
|
|
|
import Data.Nat
|
|
|
|
import Data.Fun
|
|
|
|
import Data.Rel
|
|
|
|
import Decidable.Decidable
|
|
|
|
|
2021-06-09 01:05:10 +03:00
|
|
|
%default total
|
|
|
|
|
2020-08-19 00:26:56 +03:00
|
|
|
public export
|
2021-07-13 17:32:01 +03:00
|
|
|
zeroNeverGreater : Not (LTE (S n) Z)
|
2021-02-16 14:56:43 +03:00
|
|
|
zeroNeverGreater LTEZero impossible
|
|
|
|
zeroNeverGreater (LTESucc _) impossible
|
2020-08-19 00:26:56 +03:00
|
|
|
|
|
|
|
public export
|
2021-07-09 11:06:27 +03:00
|
|
|
zeroAlwaysSmaller : {n : Nat} -> LTE Z n
|
2020-08-19 00:26:56 +03:00
|
|
|
zeroAlwaysSmaller = LTEZero
|
|
|
|
|
|
|
|
public export
|
2021-07-09 11:06:27 +03:00
|
|
|
ltesuccinjective : {0 n, m : Nat} -> Not (LTE n m) -> Not (LTE (S n) (S m))
|
2021-02-16 14:56:43 +03:00
|
|
|
ltesuccinjective disprf (LTESucc nLTEm) = void (disprf nLTEm)
|
|
|
|
ltesuccinjective disprf LTEZero impossible
|
2020-08-19 00:26:56 +03:00
|
|
|
|
2021-10-24 14:06:57 +03:00
|
|
|
-- Remove any time after release of 0.6.0
|
|
|
|
||| Deprecated. Use `Nat.isLTE`.
|
2020-08-19 00:26:56 +03:00
|
|
|
public export
|
2021-07-09 11:06:27 +03:00
|
|
|
decideLTE : (n : Nat) -> (m : Nat) -> Dec (LTE n m)
|
2021-10-24 14:06:57 +03:00
|
|
|
decideLTE = isLTE
|
2020-08-19 00:26:56 +03:00
|
|
|
|
2022-02-24 14:12:53 +03:00
|
|
|
||| If a predicate is decidable then we can decide whether it holds on
|
|
|
|
||| a bounded domain.
|
|
|
|
public export
|
|
|
|
decideLTBounded :
|
|
|
|
{0 p : Nat -> Type} ->
|
|
|
|
((n : Nat) -> Dec (p n)) ->
|
|
|
|
(n : Nat) -> Dec ((k : Nat) -> k `LT` n -> p k)
|
|
|
|
decideLTBounded pdec 0 = Yes (\ k, bnd => absurd bnd)
|
|
|
|
decideLTBounded pdec (S n) with (pdec 0)
|
|
|
|
_ | No np0 = No (\ prf => np0 (prf 0 (LTESucc LTEZero)))
|
|
|
|
_ | Yes p0 with (decideLTBounded (\ n => pdec (S n)) n)
|
|
|
|
_ | No nprf = No (\ prf => nprf (\ k, bnd => prf (S k) (LTESucc bnd)))
|
|
|
|
_ | Yes prf = Yes $ \ k, bnd =>
|
|
|
|
case view bnd of
|
|
|
|
LTZero => p0
|
|
|
|
(LTSucc bnd) => prf _ bnd
|
|
|
|
|
|
|
|
||| If a predicate is decidable then we can decide whether it holds on
|
|
|
|
||| a bounded domain.
|
|
|
|
public export
|
|
|
|
decideLTEBounded :
|
|
|
|
{0 p : Nat -> Type} ->
|
|
|
|
((n : Nat) -> Dec (p n)) ->
|
|
|
|
(n : Nat) -> Dec ((k : Nat) -> k `LTE` n -> p k)
|
|
|
|
decideLTEBounded pdec n with (decideLTBounded pdec (S n))
|
|
|
|
_ | Yes prf = Yes (\ k, bnd => prf k (LTESucc bnd))
|
|
|
|
_ | No nprf = No (\ prf => nprf (\ k, bnd => prf k (fromLteSucc bnd)))
|
|
|
|
|
2020-08-19 00:26:56 +03:00
|
|
|
public export
|
2021-07-09 11:06:27 +03:00
|
|
|
Decidable 2 [Nat,Nat] LTE where
|
2020-08-19 00:26:56 +03:00
|
|
|
decide = decideLTE
|
|
|
|
|
2021-07-26 21:12:05 +03:00
|
|
|
public export
|
|
|
|
Decidable 2 [Nat,Nat] LT where
|
|
|
|
decide m = decideLTE (S m)
|
|
|
|
|
2020-08-19 00:26:56 +03:00
|
|
|
public export
|
|
|
|
lte : (m : Nat) -> (n : Nat) -> Dec (LTE m n)
|
|
|
|
lte m n = decide {ts = [Nat,Nat]} {p = LTE} m n
|
|
|
|
|
|
|
|
public export
|
|
|
|
shift : (m : Nat) -> (n : Nat) -> LTE m n -> LTE (S m) (S n)
|
|
|
|
shift m n mLTEn = LTESucc mLTEn
|