-
Notifications
You must be signed in to change notification settings - Fork 23
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
Use newtype
instead of GADT
for LedgerProtocolParameters
#218
Use newtype
instead of GADT
for LedgerProtocolParameters
#218
Conversation
|
||
deriving instance Show (LedgerProtocolParameters era ) | ||
deriving instance Eq (LedgerProtocolParameters era) | ||
newtype LedgerProtocolParameters era = LedgerProtocolParameters |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we even need a wrapper for PParams
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Convenience. For example other types contain this type and by providing Eq
, and Show
instances, automatic instance derivation in parent types just works which is not true if you try to use the ledger types directly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But ledger types do have Eq
and Show
, don't they?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example we embed the type like this:
data TxBodyContent build era =
TxBodyContent {
txIns :: TxIns build era,
txInsCollateral :: TxInsCollateral era,
txInsReference :: TxInsReference build era,
txOuts :: [TxOut CtxTx era],
txTotalCollateral :: TxTotalCollateral era,
txReturnCollateral :: TxReturnCollateral CtxTx era,
txFee :: TxFee era,
txValidityRange :: (TxValidityLowerBound era,
TxValidityUpperBound era),
txMetadata :: TxMetadataInEra era,
txAuxScripts :: TxAuxScripts era,
txExtraKeyWits :: TxExtraKeyWitnesses era,
txProtocolParams :: BuildTxWith build (Maybe (LedgerProtocolParameters era)), -- <------ here
txWithdrawals :: TxWithdrawals build era,
txCertificates :: TxCertificates build era,
txUpdateProposal :: TxUpdateProposal era,
txMintValue :: TxMintValue build era,
txScriptValidity :: TxScriptValidity era,
txGovernanceActions :: TxGovernanceActions era,
txVotingProcedures :: Maybe (Featured ConwayEraOnwards era (VotingProcedures era))
}
deriving (Eq, Show) -- <----- automatic derivation here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But ledger types do have Eq and Show, don't they?
Yes, but because they are parameterised by ledgerera
and they demand additional constraints GHC can't figure out how to find them in automatic derivation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without newtype you'd just need a standalone deriving clause with a constraint - not much of a convenience gain imo.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My proposal: #219
instance IsShelleyBasedEra era => Eq (LedgerProtocolParameters era) where | ||
LedgerProtocolParameters a == LedgerProtocolParameters b = | ||
shelleyBasedEraConstraints (shelleyBasedEra @era) | ||
$ a == b |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See how to call the ledger version of Eq
and Show
instances you have to use shelleyBasedEraConstraints
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're just trading here EraPParams era
with IsShelleyBasedEra era
. I'm fine with both, but since we're aiming to reduce amount of newtype wrappers, not using a newtype seems a better option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trading constraints, yes, but automatic instance derivation works with IsShelleyBasedEra era
but not with EraPParams ledgerera
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For example if I try this:
data TxBodyContent build era =
TxBodyContent {
txIns :: TxIns build era,
txInsCollateral :: TxInsCollateral era,
txInsReference :: TxInsReference build era,
txOuts :: [TxOut CtxTx era],
txTotalCollateral :: TxTotalCollateral era,
txReturnCollateral :: TxReturnCollateral CtxTx era,
txFee :: TxFee era,
txValidityRange :: (TxValidityLowerBound era,
TxValidityUpperBound era),
txMetadata :: TxMetadataInEra era,
txAuxScripts :: TxAuxScripts era,
txExtraKeyWits :: TxExtraKeyWitnesses era,
txProtocolParams :: BuildTxWith build (Maybe (Ledger.PParams (ShelleyLedgerEra era))),
txWithdrawals :: TxWithdrawals build era,
txCertificates :: TxCertificates build era,
txUpdateProposal :: TxUpdateProposal era,
txMintValue :: TxMintValue build era,
txScriptValidity :: TxScriptValidity era,
txGovernanceActions :: TxGovernanceActions era,
txVotingProcedures :: Maybe (Featured ConwayEraOnwards era (VotingProcedures era))
}
deriving (Eq, Show)
I get these errors:
internal/Cardano/Api/TxBody.hs:1766:16: error:
• Could not deduce (Eq
(Ledger.PParamsHKD
Data.Functor.Identity.Identity (ShelleyLedgerEra era)))
arising from the 12th field of ‘TxBodyContent’
(type ‘BuildTxWith
build (Maybe (Ledger.PParams (ShelleyLedgerEra era)))’)
from the context: IsShelleyBasedEra era
bound by the deriving clause for ‘Eq (TxBodyContent build era)’
at internal/Cardano/Api/TxBody.hs:1766:16-17
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Eq (TxBodyContent build era))
|
1766 | deriving (Eq, Show)
| ^^
internal/Cardano/Api/TxBody.hs:1766:20: error:
• Could not deduce (Show
(Ledger.PParamsHKD
Data.Functor.Identity.Identity (ShelleyLedgerEra era)))
arising from the 12th field of ‘TxBodyContent’
(type ‘BuildTxWith
build (Maybe (Ledger.PParams (ShelleyLedgerEra era)))’)
from the context: IsShelleyBasedEra era
bound by the deriving clause for ‘Show (TxBodyContent build era)’
at internal/Cardano/Api/TxBody.hs:1766:20-23
Possible fix:
use a standalone 'deriving instance' declaration,
so you can specify the instance context yourself
• When deriving the instance for (Show (TxBodyContent build era))
|
1766 | deriving (Eq, Show)
…ution-command-fixes Ensure both proposal and constitution related commands will accept text, file and hash
Changelog
Context
A
newtype
is a thinner wrapper around the base type than a GADT. This will make accessing the contents of the type easier.There is no need to carry a witness value because the caller can supply it and there is no need to carry a constraint because the caller can use the witness to summon the constraint. In this case
shelleyBasedEraConstraints w
will do.Checklist
See Running tests for more details
.cabal
files are updatedhlint
. See.github/workflows/check-hlint.yml
to get thehlint
versionstylish-haskell
. See.github/workflows/stylish-haskell.yml
to get thestylish-haskell
versionghc-8.10.7
andghc-9.2.7