Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell98 |
RSPP
Contents
Description
This module implements a solver for the Rational Street Performer Protocol.
Typical usage:
Gather a collection of pledges. They can be for a fixed amount, or a pledge based on the total raised:
import RSPP import Data.Fixed -- for our money type type Money = Centi pledges :: [Pledge Money] pledges = [ Pledge [RationalPledge 100.00 5.00 1.00] 250.00 -- Pledge $1 for every $5 raised above $100, up to a limit of $250 , Pledge [FixedPledge 50.00, RationalPledge 25.00 2.00 1.00] 500.00 -- Pledge $50, plus $1 for every $2 raised above $25, up to a limit of $500 , Pledge [FixedPledge 200.00] 200.00 -- Pledge $200 ]
Find the total amount that these pledges will raise:
total :: Money total = solve pledges -- 725.00
Find out how much each pledge evaluates to:
pledgeAmounts :: [Money] pledgeAmounts = fmap (evalPledge total) pledges -- [125.00, 400.00, 200.00]
- data Pledge c = Pledge {
- pledgeClauses :: [PledgeClause c]
- pledgeLimit :: c
- data PledgeClause c
- = FixedPledge c
- | RationalPledge { }
- solve :: (Foldable t, Ord c, Fractional c) => t (Pledge c) -> c
- evalClause :: (Ord c, Fractional c) => c -> PledgeClause c -> c
- evalPledge :: (Ord c, Fractional c) => c -> Pledge c -> c
- evalPledges :: (Foldable t, Ord c, Fractional c) => c -> t (Pledge c) -> c
- pledgeMax :: Pledge c -> c
- pledgeMin :: (Ord c, Fractional c) => Pledge c -> c
- maxPledges :: (Foldable t, Num c) => t (Pledge c) -> c
- minPledges :: (Foldable t, Ord c, Fractional c) => t (Pledge c) -> c
Types
Pledge
A single pledge. Collect these in a Foldable (ie. List), and calculate the total with solve
.
The c
type parameter is for your currency datatype, ie. Centi
from Data.Fixed.
Constructors
Pledge | |
Fields
|
PledgeClause
data PledgeClause c Source
A clause within a pledge, describing its behaviour.
The clauses within the pledge are added together, but may never exceed the pledgeLimit
.
Constructors
FixedPledge c | Simply contribute a fixed amount |
RationalPledge | |
Instances
Eq c => Eq (PledgeClause c) | |
Read c => Read (PledgeClause c) | |
Show c => Show (PledgeClause c) |
Functions
Arguments
:: (Foldable t, Ord c, Fractional c) | |
=> t (Pledge c) | All pledges |
-> c | The maximum consistent total |
The key function: it does a binary search to find the maximum total which satisfies all pledges.
Arguments
:: (Ord c, Fractional c) | |
=> c | The total raised, including this clause |
-> PledgeClause c | The clause to evaluate |
-> c | What this clause contributes to the given total |
Given a particular total, how much did this clause contribute to that total?
Arguments
:: (Ord c, Fractional c) | |
=> c | The total raised, including this pledge |
-> Pledge c | The pledge to evaluate |
-> c | What this pledge contributes to the given total |
Given a particular total, how much of it was this pledge? Note: the given total includes the total of this pledge!
Arguments
:: (Foldable t, Ord c, Fractional c) | |
=> c | The total to evaluate against |
-> t (Pledge c) | All pledges |
-> c | The total of all pledges evaluated against the given total |
Given a particular total, what is the total of all the pledges?
Note: This may not give a valid result! Use solve
for that.
pledgeMax :: Pledge c -> c Source
The most this pledge could contribute. This currently just uses the pledge's limit, though this may not always be quite right. Consider the following pledge: Pledge [FixedPledge 10] 100 The limit of 100 will certainly never be reached.
pledgeMin :: (Ord c, Fractional c) => Pledge c -> c Source
The least this pledge may contribute. May be above 0 if it contains a FixedPledge clause.
maxPledges :: (Foldable t, Num c) => t (Pledge c) -> c Source
The highest conceivable total that these pledges may contribute, based on their limits. Note that it may not be possible to actually reach this total.
minPledges :: (Foldable t, Ord c, Fractional c) => t (Pledge c) -> c Source
The lowest conceivable total that these pledges may contribute.