Agda-2.5.2: A dependently typed functional programming language and proof assistant

Safe Haskell | None
Language | Haskell2010

Agda.Utils.Favorites

Description

Maintaining a list of favorites of some partially ordered type. Only the best elements are kept.

To avoid name clashes, import this module qualified, as in `import Agda.Utils.Favorites (Favorites) import qualified Agda.Utils.Favorites as Fav`

Synopsis

- newtype Favorites a = Favorites {
  - toList :: [a]
  }
- data CompareResult a
  - = Dominates {
    - dominated :: [a]
    - notDominated :: [a]
    }
  - | IsDominated {
    - dominator :: a
    }
- compareWithFavorites :: PartialOrd a => a -> Favorites a -> CompareResult a
- compareFavorites :: PartialOrd a => Favorites a -> Favorites a -> (Favorites a, Favorites a)
- unionCompared :: (Favorites a, Favorites a) -> Favorites a
- insertCompared :: a -> Favorites a -> CompareResult a -> Favorites a
- insert :: PartialOrd a => a -> Favorites a -> Favorites a
- union :: PartialOrd a => Favorites a -> Favorites a -> Favorites a
- fromList :: PartialOrd a => [a] -> Favorites a

# Documentation

newtype Favorites a #

A list of incomparable favorites.

Constructors

Favorites |
Fields toList :: [a]

Instances

Foldable Favorites # |
Methods fold :: Monoid m => Favorites m -> m # foldMap :: Monoid m => (a -> m) -> Favorites a -> m # foldr :: (a -> b -> b) -> b -> Favorites a -> b # foldr' :: (a -> b -> b) -> b -> Favorites a -> b # foldl :: (b -> a -> b) -> b -> Favorites a -> b # foldl' :: (b -> a -> b) -> b -> Favorites a -> b # foldr1 :: (a -> a -> a) -> Favorites a -> a # foldl1 :: (a -> a -> a) -> Favorites a -> a # toList :: Favorites a -> [a] # null :: Favorites a -> Bool # length :: Favorites a -> Int # elem :: Eq a => a -> Favorites a -> Bool # maximum :: Ord a => Favorites a -> a # minimum :: Ord a => Favorites a -> a # sum :: Num a => Favorites a -> a # product :: Num a => Favorites a -> a #
Singleton a (Favorites a) # |
Methods singleton :: a -> Favorites a #
Ord a => Eq (Favorites a) # | Equality checking is a bit expensive, since we need to sort! Maybe use a `Set` of favorites in the first place?
Methods (==) :: Favorites a -> Favorites a -> Bool # (/=) :: Favorites a -> Favorites a -> Bool #
Show a => Show (Favorites a) # |
Methods showsPrec :: Int -> Favorites a -> ShowS # show :: Favorites a -> String # showList :: [Favorites a] -> ShowS #
PartialOrd a => Semigroup (Favorites a) # | `Favorites` forms a `Monoid` under `empty` and 'union.
Methods (<>) :: Favorites a -> Favorites a -> Favorites a # sconcat :: NonEmpty (Favorites a) -> Favorites a # stimes :: Integral b => b -> Favorites a -> Favorites a #
PartialOrd a => Monoid (Favorites a) # |
Methods mempty :: Favorites a # mappend :: Favorites a -> Favorites a -> Favorites a # mconcat :: [Favorites a] -> Favorites a #
Null (Favorites a) # |
Methods empty :: Favorites a # null :: Favorites a -> Bool #

data CompareResult a #

Result of comparing a candidate with the current favorites.

Constructors

Dominates | Great, you are dominating a possibly (empty list of favorites) but there is also a rest that is not dominated. If `null dominated`, then `notDominated` is necessarily the complete list of favorites.
Fields dominated :: [a] notDominated :: [a]
IsDominated | Sorry, but you are dominated by that favorite.
Fields dominator :: a

compareWithFavorites :: PartialOrd a => a -> Favorites a -> CompareResult a #

Gosh, got some pretty `a` here, compare with my current favorites! Discard it if there is already one that is better or equal. (Skewed conservatively: faithful to the old favorites.) If there is no match for it, add it, and dispose of all that are worse than `a`.

We require a partial ordering. Less is better! (Maybe paradoxically.)

compareFavorites :: PartialOrd a => Favorites a -> Favorites a -> (Favorites a, Favorites a) #

Compare a new set of favorites to an old one and discard the new favorites that are dominated by the old ones and vice verse. (Skewed conservatively: faithful to the old favorites.)

    compareFavorites new old = (new', old')

unionCompared :: (Favorites a, Favorites a) -> Favorites a #

insertCompared :: a -> Favorites a -> CompareResult a -> Favorites a #

After comparing, do the actual insertion.

insert :: PartialOrd a => a -> Favorites a -> Favorites a #

Compare, then insert accordingly. `insert a l = insertCompared a l (compareWithFavorites a l)`

union :: PartialOrd a => Favorites a -> Favorites a -> Favorites a #

Insert all the favorites from the first list into the second.

fromList :: PartialOrd a => [a] -> Favorites a #

Construct favorites from elements of a partial order. The result depends on the order of the list if it contains equal elements, since earlier seen elements are favored over later seen equals. The first element of the list is seen first.