Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce boilerplate needed to use liftShrink functions. #2998

Merged
merged 3 commits into from
Nov 3, 2021

Conversation

jonathanknowles
Copy link
Member

@jonathanknowles jonathanknowles commented Nov 3, 2021

Issue Number

None

Background

Our test-utils package currently provides the liftShrink family of functions, which make it possible to write shrinkers in a compositional style.

These functions are especially useful when defining shrinkers for record types: if you already have a shrinker for each of the fields, then you can build a shrinker for record values by composing the individual shrinkers with an appropriate liftShrink.

However, when using these functions to shrink records, callers currently have to define boilerplate functions to convert between records and tuples.

Summary

This PR removes the need to define boilerplate tuple conversion functions.

For example, we can make the following simplification:

 shrinkSelectionParams :: SelectionParams -> [SelectionParams]
 shrinkSelectionParams =
-    shrinkMapBy ofTuple toTuple $ liftShrink9
+    liftShrink9 SelectionParams
         shrinkAssetsToBurn
         shrinkAssetsToMint
         shrinkOutputsToCover
         shrinkRewardWithdrawal
         shrinkCertificateDepositsTaken
         shrinkCertificateDepositsReturned
         shrinkCollateralRequirement
         shrinkUTxOAvailableForCollateral
         shrinkUTxOAvailableForInputs
-  where
-    toTuple (SelectionParams a b c d e f g h i) = (a, b, c, d, e, f, g, h, i)
-    ofTuple (a, b, c, d, e, f, g, h, i) = (SelectionParams a b c d e f g h i)

This module contains generic functions and types relating to data types
with one or more fields.
In this commit, we generalize the `liftShrink` family of functions so
that each of these functions can be applied to any data type with the
right number of fields.
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/generalize-liftShrink-functions branch from 42c70d4 to b65a698 Compare November 3, 2021 03:30
@jonathanknowles jonathanknowles force-pushed the jonathanknowles/generalize-liftShrink-functions branch from db76309 to b5ea488 Compare November 3, 2021 03:35
Copy link
Contributor

@sevanspowell sevanspowell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like an improvement 👍

@jonathanknowles
Copy link
Member Author

bors r+

iohk-bors bot added a commit that referenced this pull request Nov 3, 2021
2998: Reduce boilerplate needed to use `liftShrink` functions. r=jonathanknowles a=jonathanknowles

## Issue Number

None

## Background

Our `test-utils` package currently provides the `liftShrink` family of functions, which make it possible to write shrinkers in a compositional style.

These functions are especially useful when defining shrinkers for record types: if you already have a shrinker for each of the fields, then you can build a shrinker for record values by composing the individual shrinkers with an appropriate `liftShrink`.

However, when using these functions to shrink records, callers currently have to define boilerplate functions to convert between records and tuples.

## Summary

This PR removes the need to define boilerplate tuple conversion functions.

For example, we can make the following simplification:

```patch
 shrinkSelectionParams :: SelectionParams -> [SelectionParams]
 shrinkSelectionParams =
-    shrinkMapBy ofTuple toTuple $ liftShrink9
+    liftShrink9 SelectionParams
         shrinkAssetsToBurn
         shrinkAssetsToMint
         shrinkOutputsToCover
         shrinkRewardWithdrawal
         shrinkCertificateDepositsTaken
         shrinkCertificateDepositsReturned
         shrinkCollateralRequirement
         shrinkUTxOAvailableForCollateral
         shrinkUTxOAvailableForInputs
-  where
-    toTuple (SelectionParams a b c d e f g h i) = (a, b, c, d, e, f, g, h, i)
-    ofTuple (a, b, c, d, e, f, g, h, i) = (SelectionParams a b c d e f g h i)
```

Co-authored-by: Jonathan Knowles <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 3, 2021

Build failed:

Failures:
 
  src/Test/Integration/Scenario/CLI/Shelley/Transactions.hs:243:59: 
  1) CLI Specifications, SHELLEY_CLI_TRANSACTIONS, TRANS_CREATE_06 - Invalid amount, string with diacritics
       uncaught exception: IOException of type ResourceVanished
       fd:155: hFlush: resource vanished (Broken pipe)

@jonathanknowles
Copy link
Member Author

bors r+

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Nov 3, 2021

Build succeeded:

@iohk-bors iohk-bors bot merged commit 159c80a into master Nov 3, 2021
@iohk-bors iohk-bors bot deleted the jonathanknowles/generalize-liftShrink-functions branch November 3, 2021 05:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants