0 | module Data.Prim.Int
  1 |
  2 | import public Control.WellFounded
  3 | import public Data.DPair
  4 | import public Data.Prim.Ord
  5 | import public Algebra.Solver.Ring
  6 | import Syntax.PreorderReasoning
  7 |
  8 | %default total
  9 |
 10 | unsafeRefl : a === b
 11 | unsafeRefl = believe_me (Refl {x = a})
 12 |
 13 | --------------------------------------------------------------------------------
 14 | --          (<)
 15 | --------------------------------------------------------------------------------
 16 |
 17 | ||| Witness that `m < n === True`.
 18 | export
 19 | data (<) : (m,n : Int) -> Type where
 20 |   LT : {0 m,n : Int} -> (0 prf : (m < n) === True) -> m < n
 21 |
 22 | ||| Contructor for `(<)`.
 23 | |||
 24 | ||| This can only be used in an erased context.
 25 | export %hint
 26 | 0 mkLT : (0 prf : (m < n) === True) -> m < n
 27 | mkLT = LT
 28 |
 29 | ||| Extractor for `(<)`.
 30 | |||
 31 | ||| This can only be used in an erased context.
 32 | export
 33 | 0 runLT : m < n -> (m < n) === True
 34 | runLT (LT prf) = prf
 35 |
 36 | ||| We don't trust values of type `(<)` too much,
 37 | ||| so we use this when creating magical results.
 38 | export
 39 | strictLT : (0 p : m < n) -> Lazy c -> c
 40 | strictLT (LT prf) x = x
 41 |
 42 | --------------------------------------------------------------------------------
 43 | --          Aliases
 44 | --------------------------------------------------------------------------------
 45 |
 46 | ||| Flipped version of `(<)`.
 47 | public export
 48 | 0 (>) : (m,n : Int) -> Type
 49 | m > n = n < m
 50 |
 51 | ||| `m <= n` mean that either `m < n` or `m === n` holds.
 52 | public export
 53 | 0 (<=) : (m,n : Int) -> Type
 54 | m <= n = Either (m < n) (m === n)
 55 |
 56 | ||| Flipped version of `(<=)`.
 57 | public export
 58 | 0 (>=) : (m,n : Int) -> Type
 59 | m >= n = n <= m
 60 |
 61 | ||| `m /= n` mean that either `m < n` or `m > n` holds.
 62 | public export
 63 | 0 (/=) : (m,n : Int) -> Type
 64 | m /= n = Either (m < n) (m > n)
 65 |
 66 | --------------------------------------------------------------------------------
 67 | --          Order
 68 | --------------------------------------------------------------------------------
 69 |
 70 | 0 ltNotEQ : m < n -> Not (m === n)
 71 | ltNotEQ x = strictLT x $ assert_total (idris_crash "IMPOSSIBLE: LT and EQ")
 72 |
 73 | 0 ltNotGT : m < n -> Not (n < m)
 74 | ltNotGT x = strictLT x $ assert_total (idris_crash "IMPOSSIBLE: LT and GT")
 75 |
 76 | 0 eqNotLT : m === n -> Not (m < n)
 77 | eqNotLT = flip ltNotEQ
 78 |
 79 | export
 80 | comp : (m,n : Int) -> Trichotomy (<) m n
 81 | comp m n = case prim__lt_Int m n of
 82 |   0 => case prim__eq_Int m n of
 83 |     0 => GT (ltNotGT $ LT unsafeRefl) (ltNotEQ $ LT unsafeRefl) (LT unsafeRefl)
 84 |     x => EQ (eqNotLT unsafeRefl) (unsafeRefl) (eqNotLT unsafeRefl)
 85 |   x => LT (LT unsafeRefl) (ltNotEQ $ LT unsafeRefl) (ltNotGT $ LT unsafeRefl)
 86 |
 87 | export
 88 | Total Int (<) where
 89 |   trichotomy   = comp
 90 |   transLT p q  = strictLT p $ strictLT q $ LT unsafeRefl
 91 |
 92 | --------------------------------------------------------------------------------
 93 | --          Arithmetics
 94 | --------------------------------------------------------------------------------
 95 |
 96 | ||| Safe division.
 97 | export %inline
 98 | sdiv : (n,d : Int) -> (0 prf : d /= 0) => Int
 99 | sdiv n d = n `div` d
100 |
101 | ||| Safe modulo.
102 | export %inline
103 | smod : (n,d : Int) -> (0 prf : d /= 0) => Int
104 | smod n d = n `mod` d
105 |