0 | module Data.Prim.Char
  1 |
  2 | import public Algebra.Ring
  3 | import public Control.Order
  4 | import public Control.Relation
  5 | import public Control.Relation.ReflexiveClosure
  6 | import public Control.Relation.Trichotomy
  7 | import public Control.WellFounded
  8 | import public Data.Maybe0
  9 |
 10 | %default total
 11 |
 12 | unsafeRefl : a === b
 13 | unsafeRefl = believe_me (Builtin.Refl {x = a})
 14 |
 15 | ||| Witness that `m < n === True`.
 16 | export
 17 | data (<) : (m,n : Char) -> Type where
 18 |   LT : {0 m,n : Char} -> (0 prf : (m < n) === True) -> m < n
 19 |
 20 | ||| Makes a compile-time proof of `x < y` available at runtime.
 21 | |||
 22 | ||| Heads up: `(<)` is not supposed to be used or even needed at runtime,
 23 | ||| as it will be erased anymay. However, this function is sometimes
 24 | ||| required, for instance when implementing interface `Connex`.
 25 | export
 26 | unerase : (0 p : m < n) -> m < n
 27 | unerase (LT p) = LT p
 28 |
 29 | ||| Contructor for `(<)`.
 30 | |||
 31 | ||| This can only be used in an erased context.
 32 | export %hint
 33 | 0 mkLT : (0 prf : (m < n) === True) -> m < n
 34 | mkLT = LT
 35 |
 36 | ||| Extractor for `(<)`.
 37 | |||
 38 | ||| This can only be used in an erased context.
 39 | export
 40 | 0 runLT : m < n -> (m < n) === True
 41 | runLT (LT prf) = prf
 42 |
 43 | ||| We don't trust values of type `(<)` too much,
 44 | ||| so we use this when creating magical results.
 45 | export
 46 | strictLT : (0 p : m < n) -> Lazy c -> c
 47 | strictLT (LT prf) x = x
 48 |
 49 | --------------------------------------------------------------------------------
 50 | --          Aliases
 51 | --------------------------------------------------------------------------------
 52 |
 53 | ||| Flipped version of `(<)`.
 54 | public export
 55 | 0 (>) : (m,n : Char) -> Type
 56 | m > n = n < m
 57 |
 58 | ||| Alias for `ReflexiveClosure (<) m n`
 59 | public export
 60 | 0 (<=) : (m,n : Char) -> Type
 61 | (<=) = ReflexiveClosure (<)
 62 |
 63 | --------------------------------------------------------------------------------
 64 | --          Tests
 65 | --------------------------------------------------------------------------------
 66 |
 67 | 0 ltNotEQ : m < n -> Not (m === n)
 68 | ltNotEQ x = strictLT x $ assert_total (idris_crash "IMPOSSIBLE: LT and EQ")
 69 |
 70 | 0 ltNotGT : m < n -> Not (n < m)
 71 | ltNotGT x = strictLT x $ assert_total (idris_crash "IMPOSSIBLE: LT and GT")
 72 |
 73 | 0 eqNotLT : m === n -> Not (m < n)
 74 | eqNotLT = flip ltNotEQ
 75 |
 76 | public export %inline
 77 | lt : (x,y : Char) -> Maybe0 (x < y)
 78 | lt x y = case prim__lt_Char x y of
 79 |   0 => Nothing0
 80 |   _ => Just0 (mkLT unsafeRefl)
 81 |
 82 | public export %inline
 83 | lte : (x,y : Char) -> Maybe0 (x <= y)
 84 | lte x y = case prim__lte_Char x y of
 85 |   0 => Nothing0
 86 |   _ => Just0 (if x < y then (Rel $ mkLT unsafeRefl) else (fromEq unsafeRefl))
 87 |
 88 | export
 89 | comp : (m,n : Char) -> Trichotomy (<) m n
 90 | comp m n = case prim__lt_Char m n of
 91 |   0 => case prim__eq_Char m n of
 92 |     0 => GT (ltNotGT $ LT unsafeRefl) (ltNotEQ $ LT unsafeRefl) (LT unsafeRefl)
 93 |     x => EQ (eqNotLT unsafeRefl) (unsafeRefl) (eqNotLT unsafeRefl)
 94 |   x => LT (LT unsafeRefl) (ltNotEQ $ LT unsafeRefl) (ltNotGT $ LT unsafeRefl)
 95 |
 96 | --------------------------------------------------------------------------------
 97 | --          Interfaces
 98 | --------------------------------------------------------------------------------
 99 |
100 | export %inline
101 | Transitive Char (<) where
102 |   transitive _ _ = LT unsafeRefl
103 |
104 | export %inline
105 | Trichotomous Char (<) where
106 |   trichotomy m n = comp m n
107 |
108 | --------------------------------------------------------------------------------
109 | --          Bounds and Well-Foundedness
110 | --------------------------------------------------------------------------------
111 |
112 | ||| Lower bound of `Char`
113 | public export
114 | MinChar : Char
115 | MinChar = chr 0
116 |
117 | ||| `m >= 0` for all `m` of type `Char`.
118 | export
119 | 0 GTE_MinChar : (m : Char) -> MinChar <= m
120 | GTE_MinChar m = case comp MinChar m of
121 |   LT x f g => Rel x
122 |   EQ f x g => fromEq x
123 |   GT f g x => assert_total $ idris_crash "IMPOSSIBLE: Char smaller than 0"
124 |
125 | ||| Not value of type `Char` is less than zero.
126 | export
127 | 0 Not_LT_MinChar : m < MinChar -> Void
128 | Not_LT_MinChar p = Not_LT_and_GTE p (GTE_MinChar m)
129 |
130 | ||| Every value of type `Char` is accessible with relation
131 | ||| to `(<)`.
132 | export
133 | accessLT : (m : Char) -> Accessible (<) m
134 | accessLT m = Access $ \n,lt => accessLT (assert_smaller m n)
135 |
136 | namespace WellFounded
137 |   ||| `(<)` is well founded.
138 |   export %inline
139 |   [LT] WellFounded Char (<) where
140 |     wellFounded = accessLT
141 |