Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8405971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:58:14+00:00 2026-06-09T22:58:14+00:00

While writing some Arbitrary instances, I implemented a couple of functions with the following

  • 0

While writing some Arbitrary instances, I implemented a couple of functions with the following quite mechanical pattern:

type A = Arbitrary -- to cut down on the size of the annotations below
shrink1 :: (A a          ) => (a           -> r) -> (a           -> [r])
shrink2 :: (A a, A b     ) => (a -> b      -> r) -> (a -> b      -> [r])
shrink3 :: (A a, A b, A c) => (a -> b -> c -> r) -> (a -> b -> c -> [r])

shrink1 f a     = [f a'     | a' <- shrink a]
shrink2 f a b   = [f a' b   | a' <- shrink a] ++ [f a b'   | b' <- shrink b]
shrink3 f a b c = [f a' b c | a' <- shrink a] ++ [f a b' c | b' <- shrink b] ++ [f a b c' | c' <- shrink c]

I wrote out these functions by hand up to shrink7, and that seems to be sufficient for my needs. But I can’t help but wonder: can this reasonably be automated? Bonus points for a solution that:

  • allows for shrink0 f = []
  • generates all the shrinkers
  • has loads of typeclass hackery, I love that
  • skips the scary extensions like incoherent/undecidable/overlapping instances
  • lets me have my cake and eat it, too: doesn’t require me to uncurry f when passing it in or curry the application shrinkX f when applying it to a, b, and c
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:58:15+00:00Added an answer on June 9, 2026 at 10:58 pm

    This compiles, I hope it works:

    {-# LANGUAGE TypeFamilies #-}
    import Test.QuickCheck
    
    class Shrink t where
      type Inp t :: *
      shrinkn :: Inp t -> t
      (++*) :: [Inp t] -> t -> t
    
    instance Shrink [r] where
      type Inp [r] = r
      shrinkn _ = []
      (++*) = (++) 
    
    instance (Arbitrary a, Shrink s) => Shrink (a -> s) where
      type Inp (a -> s) = a -> Inp s
      shrinkn f a = [ f a' | a' <- shrink a ] ++* shrinkn (f a)
      l ++* f = \b -> map ($ b) l ++* f b
    

    (++*) is only for implementing shrinkn.

    Sorry for the relative lack of typeclass hackery. The [r] provides a nice stop condition for the type recursion, so hackery isn’t needed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was writing some php code with a couple friends of mine a while
While writing some C code, I decided to compile it to assembly and read
While writing some views to respond to ajax requests i find it somewhat strange
I have come across an annoying problem while writing some PHP4 code. I renamed
I have some troubles while writing logs from log4net to the file. I seem
I'm writing some code that looks like this: while(true) { switch(msg->state) { case MSGTYPE:
A while ago, I was writing some code, but accidentally deleted some good code
Let we explain what I mean. Some time ago, while writing a program in
While making some final tests of a class-library that I'm writing for Windows Mobile
Some background info: I am currently writing a bootloader in protected mode while learning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.