0 | module Data.Nat1
  1 |
  2 | import public Data.Nat
  3 | import public Data.DPair
  4 | import Data.CheckedEmpty.List.Lazy
  5 | import public Data.So
  6 |
  7 | %default total
  8 |
  9 | --- Type definition ---
 10 |
 11 | public export
 12 | data Nat1 : Type where
 13 |   FromNat : (n : Nat) -> (0 _ : IsSucc n) => Nat1
 14 |
 15 | --- Covertions ---
 16 |
 17 | public export
 18 | toNat1 : Nat -> Maybe Nat1
 19 | toNat1 Z       = Nothing
 20 | toNat1 k@(S _) = Just $ FromNat k
 21 |
 22 | public export %inline
 23 | fromNat : (x : Nat) -> (0 _ : IsSucc x) => Nat1
 24 | fromNat = FromNat
 25 |
 26 | public export %inline
 27 | toNat, (.asNat) : Nat1 -> Nat
 28 | toNat $ FromNat n = n
 29 | (.asNat) = toNat
 30 |
 31 | --- Implementations of basic interfaces ---
 32 |
 33 | public export
 34 | Eq Nat1 where
 35 |   FromNat n == FromNat n' = n == n'
 36 |
 37 | public export
 38 | Ord Nat1 where
 39 |   compare = comparing $ \(FromNat n) => n
 40 |
 41 | --- Literals syntax support ---
 42 |
 43 | -- This variant of implementation is a bit cheaty, but effective in terms of memory
 44 | export %inline
 45 | fromInteger : (x : Integer) -> (0 _ : So $ x >= 1) => Nat1
 46 | fromInteger x = FromNat (cast x) @{believe_me $ ItIsSucc {n=1}}
 47 |
 48 | export
 49 | Range Nat1 where
 50 |   rangeFrom fr = rangeFrom fr.asNat <&> \x => FromNat x @{believe_me $ ItIsSucc {n=1}}
 51 |   rangeFromThen fr th = rangeFromThen fr.asNat th.asNat <&> \x => FromNat x @{believe_me $ ItIsSucc {n=1}}
 52 |   rangeFromTo fr to = rangeFromTo fr.asNat to.asNat <&> \x => FromNat x @{believe_me $ ItIsSucc {n=1}}
 53 |   rangeFromThenTo fr th to = rangeFromThenTo fr.asNat th.asNat to.asNat <&> \x => FromNat x @{believe_me $ ItIsSucc {n=1}}
 54 |
 55 | --- Simple arithmetics ---
 56 |
 57 | public export %inline
 58 | succ : Nat1 -> Nat1
 59 | succ $ FromNat n = FromNat (S n)
 60 |
 61 | public export %inline
 62 | one : Nat1
 63 | one = 1
 64 |
 65 | public export %inline
 66 | (+) : Nat1 -> Nat1 -> Nat1
 67 | FromNat (S n) + FromNat (S m) = FromNat (S n + S m)
 68 |
 69 | public export %inline
 70 | (*) : Nat1 -> Nat1 -> Nat1
 71 | FromNat (S n) * FromNat (S m) = FromNat (S n * S m)
 72 |
 73 | --- Semigroups and etc ---
 74 |
 75 | namespace Semigroup
 76 |
 77 |   public export
 78 |   [Additive] Semigroup Nat1 where
 79 |     (<+>) = (+)
 80 |
 81 |   public export
 82 |   [Multiplicative] Semigroup Nat1 where
 83 |     (<+>) = (*)
 84 |
 85 | namespace Monoid
 86 |
 87 |   public export
 88 |   [Multiplicative] Monoid Nat1 using Nat1.Semigroup.Multiplicative where
 89 |     neutral = 1
 90 |
 91 |   public export
 92 |   [Maximum] Monoid Nat1 using Semigroup.Maximum where
 93 |     neutral = 1
 94 |
 95 | --- Greatest common divisor ---
 96 |
 97 | export
 98 | gcd : (a, b : Nat) -> (0 ok : Either (IsSucc a) (IsSucc b)) => Nat1
 99 | gcd Z Z       = void $ absurd ok
100 | gcd a@(S _) Z = FromNat a
101 | gcd Z b@(S _) = FromNat b
102 | gcd a (S b)   = assert_total $ gcd (S b) (modNatNZ a (S b) %search)
103 |
104 | export
105 | gcd' : Nat1 -> Nat1 -> Nat1
106 | gcd' (FromNat n) (FromNat m) = gcd n m
107 |
108 | --- Working with weighted lists ---
109 |
110 | -- TODO to make this to be dependent on some interface for lazy folding of non-empty collections
111 | export
112 | pickWeighted : LazyLst1 (Nat1, a) -> Nat -> a
113 | pickWeighted [(_, x)]                     _ = x
114 | pickWeighted wh@((FromNat n, x)::y::ys) k = if k < n then x else pickWeighted (assert_smaller wh $ y::ys) (k `minus` n)
115 |
116 | foldmne : Foldable f => (a -> a -> a) -> f a -> Maybe a
117 | foldmne g = foldl gg Nothing where
118 |   gg : Maybe a -> a -> Maybe a
119 |   gg ml r = (ml <&> \l => g l r) <|> Just r
120 |
121 | export
122 | normaliseWeights : Foldable f => Functor f => f (Nat1, a) -> f (Nat1, a)
123 | normaliseWeights xs = do
124 |   let Just $ FromNat (S d) = foldmne gcd' $ Builtin.fst <$> xs
125 |     | Nothing => xs
126 |   flip map xs $ mapFst $ \(FromNat n) => FromNat (divNatNZ n (S d) %search) @{believe_me $ ItIsSucc {n=1} {- since divisor is GCD -}}
127 |