{-# language AllowAmbiguousTypes #-}
{-# language DataKinds #-}
{-# language ExplicitForAll #-}
{-# language KindSignatures #-}
{-# language TypeFamilies #-}
{-# language TypeOperators #-}

module Arithmetic.Lt
  ( -- * Special Inequalities
    zero
    -- * Substitution
  , substituteL
  , substituteR
    -- * Increment
  , incrementL
  , incrementR
    -- * Decrement
  , decrementL
  , decrementR
    -- * Weaken
  , weakenL
  , weakenR
    -- * Composition
  , plus
  , transitive
  , transitiveNonstrictL
  , transitiveNonstrictR
    -- * Multiplication and Division
  , reciprocalA
  , reciprocalB
    -- * Convert to Inequality
  , toLteL
  , toLteR
    -- * Absurdities
  , absurd
    -- * Integration with GHC solver
  , constant
  ) where

import Arithmetic.Unsafe (type (<)(Lt),type (:=:)(Eq))
import Arithmetic.Unsafe (type (<=)(Lte))
import GHC.TypeNats (CmpNat,type (+))

import qualified GHC.TypeNats as GHC

toLteR :: (a < b) -> (a + 1 <= b)
{-# inline toLteR #-}
toLteR :: forall (a :: Nat) (b :: Nat). (a < b) -> (a + 1) <= b
toLteR a < b
Lt = forall (a :: Nat) (b :: Nat). a <= b
Lte

toLteL :: (a < b) -> (1 + a <= b)
{-# inline toLteL #-}
toLteL :: forall (a :: Nat) (b :: Nat). (a < b) -> (1 + a) <= b
toLteL a < b
Lt = forall (a :: Nat) (b :: Nat). a <= b
Lte

-- | Replace the left-hand side of a strict inequality
-- with an equal number.
substituteL :: (b :=: c) -> (b < a) -> (c < a)
{-# inline substituteL #-}
substituteL :: forall (b :: Nat) (c :: Nat) (a :: Nat).
(b :=: c) -> (b < a) -> c < a
substituteL b :=: c
Eq b < a
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Replace the right-hand side of a strict inequality
-- with an equal number.
substituteR :: (b :=: c) -> (a < b) -> (a < c)
{-# inline substituteR #-}
substituteR :: forall (b :: Nat) (c :: Nat) (a :: Nat).
(b :=: c) -> (a < b) -> a < c
substituteR b :=: c
Eq a < b
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Add a strict inequality to a nonstrict inequality.
plus :: (a < b) -> (c <= d) -> (a + c < b + d)
{-# inline plus #-}
plus :: forall (a :: Nat) (b :: Nat) (c :: Nat) (d :: Nat).
(a < b) -> (c <= d) -> (a + c) < (b + d)
plus a < b
Lt c <= d
Lte = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Add a constant to the left-hand side of both sides of
-- the strict inequality.
incrementL :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (a < b) -> (c + a < c + b)
{-# inline incrementL #-}
incrementL :: forall (c :: Nat) (a :: Nat) (b :: Nat).
(a < b) -> (c + a) < (c + b)
incrementL a < b
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Add a constant to the right-hand side of both sides of
-- the strict inequality.
incrementR :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (a < b) -> (a + c < b + c)
{-# inline incrementR #-}
incrementR :: forall (c :: Nat) (a :: Nat) (b :: Nat).
(a < b) -> (a + c) < (b + c)
incrementR a < b
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Subtract a constant from the left-hand side of both sides of
-- the inequality. This is the opposite of 'incrementL'.
decrementL :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (c + a < c + b) -> (a < b)
{-# inline decrementL #-}
decrementL :: forall (c :: Nat) (a :: Nat) (b :: Nat).
((c + a) < (c + b)) -> a < b
decrementL (c + a) < (c + b)
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Subtract a constant from the right-hand side of both sides of
-- the inequality. This is the opposite of 'incrementR'.
decrementR :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (a + c < b + c) -> (a < b)
{-# inline decrementR #-}
decrementR :: forall (c :: Nat) (a :: Nat) (b :: Nat).
((a + c) < (b + c)) -> a < b
decrementR (a + c) < (b + c)
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Add a constant to the left-hand side of the right-hand side of
-- the strict inequality.
weakenL :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (a < b) -> (a < c + b)
{-# inline weakenL #-}
weakenL :: forall (c :: Nat) (a :: Nat) (b :: Nat). (a < b) -> a < (c + b)
weakenL a < b
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Add a constant to the right-hand side of the right-hand side of
-- the strict inequality.
weakenR :: forall (c :: GHC.Nat) (a :: GHC.Nat) (b :: GHC.Nat).
  (a < b) -> (a < b + c)
{-# inline weakenR #-}
weakenR :: forall (c :: Nat) (a :: Nat) (b :: Nat). (a < b) -> a < (b + c)
weakenR a < b
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Compose two strict inequalities using transitivity.
transitive :: (a < b) -> (b < c) -> (a < c)
{-# inline transitive #-}
transitive :: forall (a :: Nat) (b :: Nat) (c :: Nat).
(a < b) -> (b < c) -> a < c
transitive a < b
Lt b < c
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Compose a strict inequality (the first argument) with a nonstrict
-- inequality (the second argument).
transitiveNonstrictR :: (a < b) -> (b <= c) -> (a < c)
{-# inline transitiveNonstrictR #-}
transitiveNonstrictR :: forall (a :: Nat) (b :: Nat) (c :: Nat).
(a < b) -> (b <= c) -> a < c
transitiveNonstrictR a < b
Lt b <= c
Lte = forall (a :: Nat) (b :: Nat). a < b
Lt

transitiveNonstrictL :: (a <= b) -> (b < c) -> (a < c)
{-# inline transitiveNonstrictL #-}
transitiveNonstrictL :: forall (a :: Nat) (b :: Nat) (c :: Nat).
(a <= b) -> (b < c) -> a < c
transitiveNonstrictL a <= b
Lte b < c
Lt = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Zero is less than one.
zero :: 0 < 1
{-# inline zero #-}
zero :: 0 < 1
zero = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Nothing is less than zero.
absurd :: n < 0 -> void
{-# inline absurd #-}
absurd :: forall (n :: Nat) void. (n < 0) -> void
absurd n < 0
Lt = forall a. [Char] -> a
errorWithoutStackTrace [Char]
"Arithmetic.Nat.absurd: n < 0"

-- | Use GHC's built-in type-level arithmetic to prove
-- that one number is less than another. The type-checker
-- only reduces 'CmpNat' if both arguments are constants.
constant :: forall a b. (CmpNat a b ~ 'LT) => (a < b)
{-# inline constant #-}
constant :: forall (a :: Nat) (b :: Nat). (CmpNat a b ~ 'LT) => a < b
constant = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Given that @m < n/p@, we know that @p*m < n@.
reciprocalA :: forall (m :: GHC.Nat) (n :: GHC.Nat) (p :: GHC.Nat).
  (m < GHC.Div n p) -> (p GHC.* m) < n
{-# inline reciprocalA #-}
reciprocalA :: forall (m :: Nat) (n :: Nat) (p :: Nat).
(m < Div n p) -> (p * m) < n
reciprocalA m < Div n p
_ = forall (a :: Nat) (b :: Nat). a < b
Lt

-- | Given that @m < roundUp(n/p)@, we know that @p*m < n@.
reciprocalB :: forall (m :: GHC.Nat) (n :: GHC.Nat) (p :: GHC.Nat).
  (m < GHC.Div (n GHC.- 1) p + 1) -> (p GHC.* m) < n
{-# inline reciprocalB #-}
reciprocalB :: forall (m :: Nat) (n :: Nat) (p :: Nat).
(m < (Div (n - 1) p + 1)) -> (p * m) < n
reciprocalB m < (Div (n - 1) p + 1)
_ = forall (a :: Nat) (b :: Nat). a < b
Lt