on master help sign in
Fixed `Data.SortedBag.contains` and added docs to Data.PosNat (#4348)

* Contains is no longer reversed * Added more docs for Posnat * Defined alterCounts and count in terms of a shared core function * Moved a comment relevant to countMap closer to it

commit94c6dc2d485950ce2e04725da5930e05abae3eb6 authorPatrick Hilhorst <patrickhilhorst97@gmail.com> dateSat 24 Feb 2018, 08:23 (9 years ago) parentc229ecc
2 files changed+31−9
libs/contrib/Data/PosNat.idr+20−2 libs/contrib/Data/SortedBag.idr+11−7
libs/contrib/Data/PosNat.idr+20−2
@@ -5,35 +5,53 @@ import Data.So
55%default total
66%access public export
77
8−||| ℤ⁺
8+||| ℤ⁺: {1..}.
99PosNat : Type
1010PosNat = DPair Nat IsSucc
1111
12−||| A proof that
12+||| A proof that the addition of a natural number to a natural number that is
13+||| already positive doesn't make a difference
1314plusPos : (n, k : Nat) -> IsSucc n -> IsSucc (n + k)
1415plusPos (S n) k pn = ItIsSucc
1516
17+|||| Add two PosNats
1618plusPosNat : PosNat -> PosNat -> PosNat
1719plusPosNat (n ** pn) (k ** _) = (n + k ** plusPos n k pn)
1820
21+||| Add a Nat to a positive Nat
1922plusNatPosNat : Nat -> PosNat -> PosNat
2023plusNatPosNat n (S k ** pk) = (S $ k + n ** ItIsSucc)
2124
25+||| The proof that one positive Nat times another is still positive
2226timesPos : (n, k : Nat) -> IsSucc n -> IsSucc k -> IsSucc (n * k)
2327timesPos n@(S _) k@(S _) pn pk = ItIsSucc
2428
29+||| Multiply two PosNats
2530multPosNat : PosNat -> PosNat -> PosNat
2631multPosNat (n ** pn) (k ** pk) = (n * k ** timesPos n k pn pk)
2732
33+||| 1
2834one : PosNat
2935one = (1 ** ItIsSucc)
3036
37+||| The successor of a PosNat
3138succ : PosNat -> PosNat
3239succ (n ** _) = (S n ** ItIsSucc)
3340
41+||| Semigroup using addition (this seems to be the more generally useful case,
42+||| even though it doesn't form a Monoid)
3443Semigroup PosNat where
3544 (<+>) = plusPosNat
3645
46+||| Semigroup using multiplication
47+[MultPosNatSemi] Semigroup PosNat where
48+ (<+>) = multPosNat
49+
50+||| Monoid, neutral = 1
51+[MultPosNatMonoid] Monoid PosNat using MultPosNatSemi where
52+ neutral = one
53+
54+||| Convert a Nat to a PosNat, using automatic proof search
3755p : (n : Nat) -> {auto ok : IsSucc n} -> PosNat
3856p Z {ok} impossible
3957p k {ok} = (k ** ok)
libs/contrib/Data/SortedBag.idr+11−7
@@ -38,21 +38,25 @@ onMap = onMaps {n=1}
3838empty : Ord k => SortedBag k
3939empty = BagWrapper empty
4040
41+||| The type of `insert` and the common core of most functions that update
42+||| the contests of a Bag.
43+UpdateBag : Type -> Type
44+UpdateBag k = k -> SortedBag k -> SortedBag k
45+
4146-- Keep in mind:
4247-- Idris> the Nat $ sum Nothing
4348-- 0 : Nat
4449-- Idris> the Nat $ sum $ Just 10
4550-- 10 : Nat
4651
47−||| The type of `insert` and the common core of most functions that update
48−||| the contests of a Bag.
49−UpdateBag : Type -> Type
50−UpdateBag k = k -> SortedBag k -> SortedBag k
52+||| Perform a count on the underlying `SortedMap`
53+countMap : k -> SortedMap k PosNat -> Nat
54+countMap k m = sum $ fst <$> lookup k m
5155
5256||| Apply a function `(Nat -> Nat)` to a given argument
5357alterCounts : (Nat -> Nat) -> UpdateBag k
5458alterCounts f k = onMap $ \m => let
55− n = sum $ fst <$> lookup k m
59+ n = countMap k m
5660 n' = f n
5761 in m |> if n == n'
5862 then id -- No change before and after.
@@ -62,11 +66,11 @@ alterCounts f k = onMap $ \m => let
6266
6367||| Count the number of occurances
6468count : k -> SortedBag k -> Nat
65−count k (BagWrapper m) = sum $ fst <$> lookup k m
69+count k (BagWrapper m) = countMap k m
6670
6771||| Is a count non-zero?
6872contains : k -> SortedBag k -> Bool
69−contains = isZero ... count
73+contains = isSucc ... count
7074
7175||| insert a given number of items
7276insertN : Nat -> UpdateBag k