nick8325 / nick8325/quickcheck

Add generating monotonic functions

Open
#348 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Haskell
Stars
790
Forks
130
Avg merge
16h 41m
Merged PRs (30d)
2

Description

I was able to implement an arbitrary monotonic function like this:

-- | A modifier to generate monotonic functions
newtype Monotonic a b = Monotonic (a -> b)

instance Show (Monotonic a b) where
  show _ = "<monotonic fun>"

instance (Show b, Real a, Arbitrary b, Fractional b, Real b, Ord b) => Arbitrary (Monotonic a b) where
  arbitrary = do
    x <- arbitrary
    ups <- infiniteListMonotonic
    downs <- infiniteListMonotonic
    pure $ Monotonic $ \a ->
      let (n, frac) = properFraction $ toRational a
          (getIndex, index) =
            if n >= 0
              then (\i -> x + (ups !! i), n)
              else (\i -> x - (downs !! i), (-n))
          lo = getIndex index
          hi = getIndex (index + 1)
       in lo + fromRational frac * (hi - lo)
    where
      infiniteListMonotonic = scanl1 (+) . map getNonNegative <$> infiniteList

It would be great if this could be included in the base library, potentially with extra features like:

  1. Shrinking, the same way Fun does
  2. Make a new type class to support outputs that are integral or non-numerical
    • e.g. for Double -> Int, interpolate between the two ints, then round or truncate
  3. Make a new type class to support inputs that are still orderable, but non-numerical
    • e.g.
    data Octal = O0 | O1 | O2 | O3 | O4 | O5 | O6 | O7
    
    arbitrary :: Gen (Octal -> Int)
    
  4. More arbitrary interpolation
    • In this snippet, fractional indices do a linear interpolation between the two arguments, which is sufficient for my (most?) purposes

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No repository file or test is named in the issue. Start by reviewing the existing Fun support in QuickCheck and the proposed Monotonic implementation, then define which generation, shrinking, input/output type, and interpolation features are in scope before implementation; done means the agreed monotonic-function support is included with corresponding coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
testing-qa
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.