Skip to content

Commit

Permalink
Use IsList(toList,fromList) instead of specialised functions
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed Aug 6, 2024
1 parent 3d000e0 commit 7e9fcb7
Show file tree
Hide file tree
Showing 24 changed files with 118 additions and 113 deletions.
3 changes: 2 additions & 1 deletion cardano-api/gen/Test/Gen/Cardano/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Cardano.Ledger.Shelley.TxAuxData (Metadatum (..), ShelleyTxAuxD

import qualified Data.Map.Strict as Map
import Data.Word (Word64)
import GHC.Exts (IsList (..))

import Test.Gen.Cardano.Api.Typed (genCostModel, genRational)

Expand All @@ -32,7 +33,7 @@ genMetadata = do
numberOfIndices <- Gen.integral (Range.linear 1 15)
let indices = map (\i -> fromIntegral i :: Word64) [1 .. numberOfIndices]
mData <- Gen.list (Range.singleton numberOfIndices) genMetadatum
return . ShelleyTxAuxData . Map.fromList $ zip indices mData
return . ShelleyTxAuxData . fromList $ zip indices mData

genMetadatum :: Gen Metadatum
genMetadatum = do
Expand Down
7 changes: 3 additions & 4 deletions cardano-api/gen/Test/Gen/Cardano/Api/Metadata.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ import qualified Data.Aeson.Key as Aeson
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.Map.Strict as Map
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)
import GHC.Exts (IsList (..))

import Hedgehog (Gen)
import qualified Hedgehog.Gen as Gen
Expand All @@ -36,8 +36,7 @@ genJsonForTxMetadata mapping =
Aeson.object
<$> Gen.list
(Range.linear 0 (fromIntegral sz))
( (,)
<$> (Aeson.fromString . show <$> Gen.word64 Range.constantBounded)
( ((,) . Aeson.fromString . show <$> Gen.word64 Range.constantBounded)
<*> genJsonForTxMetadataValue mapping
)

Expand Down Expand Up @@ -167,7 +166,7 @@ genJsonForTxMetadataValue TxMetadataJsonDetailedSchema = genJsonValue
genTxMetadata :: Gen TxMetadata
genTxMetadata =
Gen.sized $ \sz ->
TxMetadata . Map.fromList
TxMetadata . fromList
<$> Gen.list
(Range.linear 0 (fromIntegral sz))
( (,)
Expand Down
6 changes: 2 additions & 4 deletions cardano-api/internal/Cardano/Api/Certificate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.IP (IPv4, IPv6)
import Data.Maybe
import qualified Data.Sequence.Strict as Seq
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
Expand Down Expand Up @@ -610,10 +608,10 @@ toShelleyPoolParams
(Ledger.boundRational stakePoolMargin)
, Ledger.ppRewardAccount = toShelleyStakeAddr stakePoolRewardAccount
, Ledger.ppOwners =
Set.fromList
fromList
[kh | StakeKeyHash kh <- stakePoolOwners]
, Ledger.ppRelays =
Seq.fromList
fromList
(map toShelleyStakePoolRelay stakePoolRelays)
, Ledger.ppMetadata =
toShelleyPoolMetadata
Expand Down
4 changes: 2 additions & 2 deletions cardano-api/internal/Cardano/Api/Convenience/Construction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ import qualified Cardano.Ledger.Keys as L
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import qualified Data.Text as Text
import GHC.Exts (IsList (..))

-- | Construct a balanced transaction.
-- See Cardano.Api.Convenience.Query.queryStateForBalancedTx for a
Expand Down Expand Up @@ -120,7 +120,7 @@ renderNotScriptLockedTxInsError (ScriptLockedTxIns txins) =

notScriptLockedTxIns :: [TxIn] -> UTxO era -> Either ScriptLockedTxInsError ()
notScriptLockedTxIns collTxIns (UTxO utxo) = do
let onlyCollateralUTxOs = Map.restrictKeys utxo $ Set.fromList collTxIns
let onlyCollateralUTxOs = Map.restrictKeys utxo $ fromList collTxIns
scriptLockedTxIns =
filter (\(_, TxOut aInEra _ _ _) -> not $ isKeyAddress aInEra) $ Map.assocs onlyCollateralUTxOs
if null scriptLockedTxIns
Expand Down
9 changes: 4 additions & 5 deletions cardano-api/internal/Cardano/Api/Convenience/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ import Data.Map (Map)
import qualified Data.Map as Map
import Data.Maybe (mapMaybe)
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Text (Text)
import GHC.Exts (IsString (..))
import GHC.Exts (IsList (..), IsString (..))

data QueryConvenienceError
= AcqFailure AcquiringFailure
Expand Down Expand Up @@ -122,12 +121,12 @@ queryStateForBalancedTx era allTxIns certs = runExceptT $ do
requireShelleyBasedEra era
& onNothing (left ByronEraNotSupported)

let stakeCreds = Set.fromList $ mapMaybe filterUnRegCreds certs
drepCreds = Set.fromList $ mapMaybe filterUnRegDRepCreds certs
let stakeCreds = fromList $ mapMaybe filterUnRegCreds certs
drepCreds = fromList $ mapMaybe filterUnRegDRepCreds certs

-- Query execution
utxo <-
lift (queryUtxo sbe (QueryUTxOByTxIn (Set.fromList allTxIns)))
lift (queryUtxo sbe (QueryUTxOByTxIn (fromList allTxIns)))
& onLeft (left . QceUnsupportedNtcVersion)
& onLeft (left . QueryEraMismatch)

Expand Down
12 changes: 6 additions & 6 deletions cardano-api/internal/Cardano/Api/Fees.hs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ evaluateTransactionExecutionUnitsShelley sbe systemstart epochInfo (LedgerProtoc
(Either (L.TransactionScriptFailure (ShelleyLedgerEra era)) (EvalTxExecutionUnitsLog, Alonzo.ExUnits))
-> Map ScriptWitnessIndex (Either ScriptExecutionError (EvalTxExecutionUnitsLog, ExecutionUnits))
fromLedgerScriptExUnitsMap aOnwards exmap =
Map.fromList
fromList
[ ( toScriptIndex aOnwards rdmrptr
, bimap (fromAlonzoScriptExecutionError aOnwards) (second fromAlonzoExUnits) exunitsOrFailure
)
Expand Down Expand Up @@ -1349,7 +1349,7 @@ createFakeUTxO sbe txbodycontent totalAdaInUTxO =
txOuts txbodycontent
in -- Take one txin and one txout. Replace the out value with totalAdaInUTxO
-- Return an empty UTxO if there are no txins or txouts
UTxO $ Map.fromList $ zip singleTxIn singleTxOut
UTxO $ fromList $ zip singleTxIn singleTxOut

updateTxOut :: ShelleyBasedEra era -> Coin -> TxOut CtxUTxO era -> TxOut CtxUTxO era
updateTxOut sbe updatedValue txout =
Expand Down Expand Up @@ -1567,7 +1567,7 @@ substituteExecutionUnits

return $
Just
(Featured era (TxVotingProcedures vProcedures (BuildTxWith $ Map.fromList substitutedExecutionUnits)))
(Featured era (TxVotingProcedures vProcedures (BuildTxWith $ fromList substitutedExecutionUnits)))

mapScriptWitnessesProposals
:: Maybe (Featured ConwayEraOnwards era (TxProposalProcedures build era))
Expand All @@ -1592,7 +1592,7 @@ substituteExecutionUnits
Just
( Featured
era
(TxProposalProcedures osetProposalProcedures (BuildTxWith $ Map.fromList substitutedExecutionUnits))
(TxProposalProcedures osetProposalProcedures (BuildTxWith $ fromList substitutedExecutionUnits))
)

mapScriptWitnessesMinting
Expand All @@ -1605,7 +1605,7 @@ substituteExecutionUnits
value
(BuildTxWith witnesses)
) =
-- TxMintValue supported value $ BuildTxWith $ Map.fromList
-- TxMintValue supported value $ BuildTxWith $ fromList
let mappedScriptWitnesses
:: [(PolicyId, Either (TxBodyErrorAutoBalance era) (ScriptWitness WitCtxMint era))]
mappedScriptWitnesses =
Expand All @@ -1619,7 +1619,7 @@ substituteExecutionUnits
in do
final <- traverseScriptWitnesses mappedScriptWitnesses
Right . TxMintValue supported value . BuildTxWith $
Map.fromList final
fromList final

traverseScriptWitnesses
:: [(a, Either (TxBodyErrorAutoBalance era) (ScriptWitness ctx era))]
Expand Down
5 changes: 3 additions & 2 deletions cardano-api/internal/Cardano/Api/Governance/Poll.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import qualified Data.Text.Lazy as Text.Lazy
import qualified Data.Text.Lazy.Builder as Text.Builder
import Data.Word (Word64)
import Formatting (build, sformat)
import GHC.Exts (IsList (..))

-- | Associated metadata label as defined in CIP-0094
pollMetadataLabel :: Word64
Expand Down Expand Up @@ -124,7 +125,7 @@ instance HasTypeProxy GovernancePoll where
instance AsTxMetadata GovernancePoll where
asTxMetadata GovernancePoll{govPollQuestion, govPollAnswers, govPollNonce} =
makeTransactionMetadata $
Map.fromList
fromList
[
( pollMetadataLabel
, TxMetaMap $
Expand Down Expand Up @@ -220,7 +221,7 @@ instance HasTypeProxy GovernancePollAnswer where
instance AsTxMetadata GovernancePollAnswer where
asTxMetadata GovernancePollAnswer{govAnsPoll, govAnsChoice} =
makeTransactionMetadata $
Map.fromList
fromList
[
( pollMetadataLabel
, TxMetaMap
Expand Down
7 changes: 4 additions & 3 deletions cardano-api/internal/Cardano/Api/LedgerState.hs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Lazy as LBS
import Data.ByteString.Short as BSS
import Data.Foldable
import Data.Foldable (asum)
import Data.IORef
import qualified Data.List as List
import Data.Map.Strict (Map)
Expand All @@ -219,6 +219,7 @@ import Data.Text.Lazy.Builder (toLazyText)
import Data.Word
import qualified Data.Yaml as Yaml
import Formatting.Buildable (build)
import GHC.Exts (IsList (..))
import Lens.Micro
import Network.TypedProtocol.Pipelined (Nat (..))
import System.FilePath
Expand Down Expand Up @@ -1885,7 +1886,7 @@ nextEpochEligibleLeadershipSlots sbe sGen serCurrEpochState ptclState poolid (Vr
slotRangeOfInterest pp' =
Set.filter
(not . Ledger.isOverlaySlot firstSlotOfEpoch (pp' ^. Core.ppDG))
$ Set.fromList [firstSlotOfEpoch .. lastSlotofEpoch]
$ fromList [firstSlotOfEpoch .. lastSlotofEpoch]

caseShelleyToAlonzoOrBabbageEraOnwards
( const
Expand Down Expand Up @@ -1999,7 +2000,7 @@ currentEpochEligibleLeadershipSlots sbe sGen eInfo pp ptclState poolid (VrfSigni
slotRangeOfInterest pp' =
Set.filter
(not . Ledger.isOverlaySlot firstSlotOfEpoch (pp' ^. Core.ppDG))
$ Set.fromList [firstSlotOfEpoch .. lastSlotofEpoch]
$ fromList [firstSlotOfEpoch .. lastSlotofEpoch]

caseShelleyToAlonzoOrBabbageEraOnwards
( const
Expand Down
9 changes: 9 additions & 0 deletions cardano-api/internal/Cardano/Api/Orphans.hs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,14 @@ import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Short as SBS
import Data.Data (Data)
import Data.Kind (Constraint, Type)
import Data.ListMap (ListMap)
import qualified Data.ListMap as ListMap
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Monoid
import qualified Data.Text as T
import qualified Data.Text.Encoding as Text
import Data.Typeable (Typeable)
import GHC.Exts (IsList (..))
import GHC.Generics
import GHC.Stack (HasCallStack)
import GHC.TypeLits
Expand Down Expand Up @@ -571,3 +574,9 @@ parsePlutusParamName t =
Nothing -> fail $ "Cannot parse cost model parameter name: " <> T.unpack t

deriving instance Show V2.ParamName

-- TODO upstream to cardano-ledger
instance IsList (ListMap k a) where
type Item (ListMap k a) = (k, a)
fromList = ListMap.fromList
toList = ListMap.toList
10 changes: 5 additions & 5 deletions cardano-api/internal/Cardano/Api/ProtocolParameters.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}

{- HLINT ignore "Redundant ==" -}
{- HLINT ignore "Use mapM" -}
{-# HLINT ignore "Redundant ==" #-}

-- | The various Cardano protocol parameters, including:
--
Expand Down Expand Up @@ -1016,7 +1016,7 @@ toAlonzoCostModels
-> Either ProtocolParametersConversionError Alonzo.CostModels
toAlonzoCostModels m = do
f <- mapM conv $ toList m
Right $ Plutus.mkCostModels $ Map.fromList f
Right $ Plutus.mkCostModels $ fromList f
where
conv
:: (AnyPlutusScriptVersion, CostModel)
Expand All @@ -1029,7 +1029,7 @@ fromAlonzoCostModels
:: Plutus.CostModels
-> Map AnyPlutusScriptVersion CostModel
fromAlonzoCostModels cModels =
Map.fromList
fromList
. map (bimap fromAlonzoScriptLanguage fromAlonzoCostModel)
$ toList
$ Plutus.costModelsValid cModels
Expand Down Expand Up @@ -1091,7 +1091,7 @@ makeShelleyUpdateProposal params genesisKeyHashes =
-- TODO decide how to handle parameter validation
-- for example we need to validate the Rational values can convert
-- into the UnitInterval type ok.
UpdateProposal (Map.fromList [(kh, params) | kh <- genesisKeyHashes])
UpdateProposal (fromList [(kh, params) | kh <- genesisKeyHashes])

-- ----------------------------------------------------------------------------
-- Conversion functions: updates to ledger types
Expand Down
14 changes: 7 additions & 7 deletions cardano-api/internal/Cardano/Api/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ instance
parseJSON = withObject "UTxO" $ \hm -> do
let l = toList $ KeyMap.toHashMapText hm
res <- mapM toTxIn l
pure . UTxO $ Map.fromList res
pure . UTxO $ fromList res
where
toTxIn :: (Text, Aeson.Value) -> Parser (TxIn, TxOut CtxUTxO era)
toTxIn (txinText, txOutVal) = do
Expand Down Expand Up @@ -473,7 +473,7 @@ toShelleyAddrSet
-> Set AddressAny
-> Set (Shelley.Addr Consensus.StandardCrypto)
toShelleyAddrSet era =
Set.fromList
fromList
. map toShelleyAddr
-- Ignore any addresses that are not appropriate for the era,
-- e.g. Shelley addresses in the Byron era, as these would not
Expand All @@ -489,7 +489,7 @@ toLedgerUTxO
toLedgerUTxO sbe (UTxO utxo) =
shelleyBasedEraConstraints sbe
$ Shelley.UTxO
. Map.fromList
. fromList
. map (bimap toShelleyTxIn (toShelleyTxOut sbe))
. toList
$ utxo
Expand All @@ -502,7 +502,7 @@ fromLedgerUTxO
fromLedgerUTxO sbe (Shelley.UTxO utxo) =
shelleyBasedEraConstraints sbe
$ UTxO
. Map.fromList
. fromList
. map (bimap fromShelleyTxIn (fromShelleyTxOut sbe))
. toList
$ utxo
Expand All @@ -513,7 +513,7 @@ fromShelleyPoolDistr
fromShelleyPoolDistr =
-- TODO: write an appropriate property to show it is safe to use
-- Map.fromListAsc or to use Map.mapKeysMonotonic
Map.fromList
fromList
. map (bimap StakePoolKeyHash Consensus.individualPoolStake)
. toList
. Consensus.unPoolDistr
Expand All @@ -528,7 +528,7 @@ fromShelleyDelegations =
-- Map.fromListAsc or to use Map.mapKeysMonotonic
-- In this case it may not be: the Ord instances for Shelley.Credential
-- do not match the one for StakeCredential
Map.fromList
fromList
. map (bimap fromShelleyStakeCredential StakePoolKeyHash)
. toList

Expand All @@ -538,7 +538,7 @@ fromShelleyRewardAccounts
fromShelleyRewardAccounts =
-- TODO: write an appropriate property to show it is safe to use
-- Map.fromListAsc or to use Map.mapKeysMonotonic
Map.fromList
fromList
. map (first fromShelleyStakeCredential)
. toList

Expand Down
3 changes: 1 addition & 2 deletions cardano-api/internal/Cardano/Api/Rewards.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import qualified Data.Aeson.Types as Aeson
import Data.List (nub)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import qualified Data.Vector as Vector
import GHC.Exts (IsList (..))

-- | A mapping of Shelley reward accounts to both the stake pool that they
Expand All @@ -27,7 +26,7 @@ newtype DelegationsAndRewards
instance ToJSON DelegationsAndRewards where
toJSON delegsAndRwds =
Aeson.Array
. Vector.fromList
. fromList
. map delegAndRwdToJson
$ mergeDelegsAndRewards delegsAndRwds
where
Expand Down
Loading

0 comments on commit 7e9fcb7

Please sign in to comment.