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

Make tests polymorphic over the Value type #1913

Merged
merged 2 commits into from
Oct 20, 2020
Merged

Conversation

polinavino
Copy link
Contributor

@polinavino polinavino commented Oct 13, 2020

  • Remove the constraint Core.Value era ~ Coin from the bundle ShelleyTest

  • Add to ShelleyTest testing-related constraints on the Value type (Arbitrary, hedgehog, Split)

  • Change ShelleyBased from a constraint bundle to a type class

  • Remove UndecidableInstances where they're not needed

  • Update generators (genTxOut) of outputs to generate ones containing Value era

  • Rename Value type family to VALUE following some (apparently existing) convention for type family names, then wrap VALUE in Value newtype

  • Split out Compatible as a module

  • Leave testing files where ConcreteCryptoTypes (C) is imported as using the C era

Copy link
Contributor

@nc6 nc6 left a comment

Choose a reason for hiding this comment

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

Just did a partial review to leave some comments (now stuck in meetings)! Might be good to go through this in a meeting since there's probably some subtleties to it

@@ -33,4 +33,4 @@ type family MAValue (x :: MaryOrAllegra) era :: Type where
MAValue Allegra era = Coin
MAValue Mary era = Value era

type instance Core.Value (ShelleyMAEra m c) = MAValue m (ShelleyMAEra m c)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is Value now in ALLCAPS?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ha! we (Alex, Tim and I) discussed type families being in all caps (there is already some attempt to make this a convention it looks like from before), then wrapped in a newtype.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leaving for this PR

@@ -19,6 +19,7 @@ flag development
library
exposed-modules:
Cardano.Ledger.Core
Cardano.Ledger.Compactible
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

-- the genericShrink has something that
-- detects that the immediate subterms of a type are the same as the parent type
-- when there is a type family in that position, the instance resolution fails
newtype Value era = Value {unVl :: VALUE era}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. Somehow I seem to have got around this issue in #1908, though in honesty I haven't the foggiest how. It seems an unfortunate hack, so I'd hope we could avoid it, but maybe that just causes more trouble...

Copy link
Contributor Author

@polinavino polinavino Oct 14, 2020

Choose a reason for hiding this comment

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

I think it is because you have put ShelleyEra c where it was just era before, together with the ShelleyBased era constraint. ShelleyEra includes Core.Value~Coin and TxBody being the Shelley TxBody, so genericShrink was not confused any more

Copy link
Contributor Author

Choose a reason for hiding this comment

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

maybe rewrite the shrinker to a manual shrinker? to avoid this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leaving shrinker as is for now

@@ -14,46 +16,61 @@ module Cardano.Ledger.Core
( -- * Compactible
Compactible (..),
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest now that Compactible is in its own module (which is great!) we avoid re-exporting it from here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

mistake

@@ -146,7 +146,7 @@ deriving stock instance
ShelleyBased era =>
Eq (UtxoPredicateFailure era)

instance NoThunks (Core.Value era) => NoThunks (UtxoPredicateFailure era)
instance ShelleyBased era => NoThunks (UtxoPredicateFailure era)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd argue that cases like this (where there's only one constraint, so we don't save space by using the bundle) have a strong argument for remaining with the more specific constraint. It's better as documentation (we see exactly what we're relying on) and we have more chance of being able to re-use this in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

to remove UndecidableInstances. So I should put it back?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes

deltaT8 :: Coin
deltaT8 = Coin 317333333333

-- TODO make DeltaCoin?
deltaR8 :: Coin
deltaR8 = Coin (-330026666665)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I hope this not being a delta coin is the reason for the test failure! Coins must always be positive or there will be ghosts..

Copy link
Contributor Author

Choose a reason for hiding this comment

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

changed this to +ve value and subtraction at use site for now!


type TxBodyConstraints era =
class
Copy link
Contributor Author

Choose a reason for hiding this comment

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

put back to type

@@ -88,6 +90,7 @@ instance CryptoClass.Crypto BenchCrypto where
type ADDRHASH BenchCrypto = Blake2b_224

type BenchEra = ShelleyEra BenchCrypto
type instance VALUE BenchEra = Coin

Copy link
Contributor Author

Choose a reason for hiding this comment

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

consider changing convention for type families (or get rid of newtype)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

always use qualified imports for type families

genValList :: (ShelleyTest era) => Integer -> Integer -> Int -> Gen [Core.Value era]
genValList minCoin maxCoin len = do
let addWOCoin c v = c <+> (v <-> (Val.inject $ Val.coin v))
replicateM len $ liftM2 addWOCoin (Val.inject <$> genCoin minCoin maxCoin) QC.arbitrary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pass generator for Val instead of using arbitrary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

look at valprop.hs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leaving for now

instance Split Coin where
vsplit (Coin n) 0 = ([], Coin n)
vsplit (Coin n) m -- TODO fix this?
| m Prelude.<= 0 = error "must split coins into positive parts"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

fix what?

Core.Value era ~ Coin,
type ShelleyTest era = (ShelleyBased era, Arbitrary (Core.Value era),
HedGen (Core.Value era), Split (Core.Value era), P.PartialOrd (Core.VALUE era),
Arbitrary (Core.VALUE era), HedGen (Core.VALUE era), Split (Core.VALUE era) )
Copy link
Contributor Author

Choose a reason for hiding this comment

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

remove partial order and add a a constraint that requires to have pointwise comparison

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed

instance Arbitrary Coin where
-- Cannot be negative even though it is an 'Integer'
arbitrary = Coin <$> choose (0, 1000)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

plumbing in our own generators should make us able to remove these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

leaving for now

extraCoin sr = Val.coin $ extraTokens sr
-- put all the non-ada tokens in the head of the outputs, append shrunk list
mvExtraTksnToOut1 Empty = empty
mvExtraTksnToOut1 sr = (<|) (TxOut a (vs <+> (extraTokens sr) <-> (Val.inject $ extraCoin sr))) sr
Copy link
Contributor Author

Choose a reason for hiding this comment

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

infix

shrinkTxOut (TxOut addr coin) =
TxOut addr <$> shrinkCoin coin
shrinkTxOut (TxOut addr vl) =
TxOut addr <$> shrink vl

Copy link
Contributor Author

Choose a reason for hiding this comment

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

pass in our own shrinker here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO comment that we dont shrink the value here

@@ -36,9 +36,12 @@ module Test.Shelley.Spec.Ledger.Utils
MultiSigPairs,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

paused review here

@nc6
Copy link
Contributor

nc6 commented Oct 19, 2020

PR #1922 may also now be helpful here!

Copy link
Contributor

@JaredCorduan JaredCorduan left a comment

Choose a reason for hiding this comment

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

LGTM

@JaredCorduan JaredCorduan merged commit 43bde81 into master Oct 20, 2020
@iohk-bors iohk-bors bot deleted the polina/paramtests branch October 20, 2020 14:01
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.

3 participants