Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 30 additions & 45 deletions plutus-ledger-api/src/PlutusLedgerApi/Common/Versions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,17 @@ ledgerLanguagesAvailableIn :: MajorProtocolVersion -> Set.Set PlutusLedgerLangua
ledgerLanguagesAvailableIn searchPv =
Set.fromList $ takeWhile (\ll -> ledgerLanguageIntroducedIn ll <= searchPv) enumerate

-- | Given a map from PVs to a type `a`, return a `Set a` containing all of the
-- entries with PV <= thisPv
-- | Given a map from (LL, PV) pairs to a type `a`, return a `Set a` containing all of the
-- entries with LL = thisLL and PV <= thisPv
collectUpTo
:: Ord a
=> Map.Map MajorProtocolVersion (Set.Set a)
=> Map.Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set.Set a)
-> PlutusLedgerLanguage
-> MajorProtocolVersion
-> Set.Set a
collectUpTo m thisPv =
fold $ -- ie, iterated `union`
Map.elems $ Map.takeWhileAntitone (<= thisPv) m
collectUpTo m thisLL thisPV =
fold $ Map.elems $ Map.filterWithKey (\(ll,pv) _ -> ll == thisLL && pv <= thisPV) m
-- Map.elems $ Map.takeWhileAntitone (<= thisPv) m

{- Batches of builtins which were introduced in the same hard fork (but perhaps
not for all LLs): see the Plutus Core specification and
Expand Down Expand Up @@ -227,58 +228,42 @@ batch6 =
where no new builtins are added. See Note [New builtins/language versions and
protocol versions]
-}
builtinsIntroducedIn :: PlutusLedgerLanguage -> Map.Map MajorProtocolVersion (Set.Set DefaultFun)
builtinsIntroducedIn :: Map.Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set.Set DefaultFun)
builtinsIntroducedIn =
\case
PlutusV1 ->
Map.fromList
[ (alonzoPV, Set.fromList batch1)
, (pv11PV, Set.fromList (batch2 ++ batch3 ++ batch4 ++ batch5 ++ batch6))
]
PlutusV2 ->
Map.fromList
[ (vasilPV, Set.fromList (batch1 ++ batch2))
, (valentinePV, Set.fromList batch3)
, (plominPV, Set.fromList batch4b)
, (pv11PV , Set.fromList (batch4a ++ batch5 ++ batch6))
]
PlutusV3 ->
Map.fromList
[ (changPV, Set.fromList (batch1 ++ batch2 ++ batch3 ++ batch4))
, (plominPV, Set.fromList batch5)
, (pv11PV, Set.fromList batch6)
]
Map.fromList
[ ((PlutusV1, alonzoPV), Set.fromList batch1)
, ((PlutusV1, pv11PV), Set.fromList (batch2 ++ batch3 ++ batch4 ++ batch5 ++ batch6))
, ((PlutusV2, vasilPV), Set.fromList (batch1 ++ batch2))
, ((PlutusV2, valentinePV), Set.fromList batch3)
, ((PlutusV2, plominPV), Set.fromList batch4b)
, ((PlutusV2, pv11PV) , Set.fromList (batch4a ++ batch5 ++ batch6))
, ((PlutusV3, changPV), Set.fromList (batch1 ++ batch2 ++ batch3 ++ batch4))
, ((PlutusV3, plominPV), Set.fromList batch5)
, ((PlutusV3, pv11PV), Set.fromList batch6)
]
Copy link
Contributor

Choose a reason for hiding this comment

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

It's fine, but I kinda liked previous version more, which you can make efficient if you follow this.

Copy link
Contributor Author

@kwxm kwxm Jul 28, 2025

Choose a reason for hiding this comment

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

I had to lift the separate batches out to the top level so that I could export them for use in the tests. I'll come back and try to improve the efficiency in a separate PR: I merged the less efficient version first so that I could experiment separately.

Oh wait, do you mean the version with the funciton rather than the map? It was originally a map but I switched to a function and that seems to have made it less efficient, so I was trying to confirm that and it seesm to be true. I'm experimenting with other ways to improve efficency in other branches.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I mean you can keep it a function and have it efficient too.


{- | Return a set containing the builtins which are available in a given LL in a
given PV. All builtins are available in all LLs from `pv11PV` onwards. -}
builtinsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set.Set DefaultFun
builtinsAvailableIn = collectUpTo . builtinsIntroducedIn
builtinsAvailableIn = collectUpTo builtinsIntroducedIn


{-| A map indicating which Plutus Core versions were introduced in which
'MajorProtocolVersion' and 'PlutusLedgerLanguage'. Each version should appear at most once.
This __must__ be updated when new versions are added.
See Note [New builtins/language versions and protocol versions]
-}
plcVersionsIntroducedIn :: PlutusLedgerLanguage -> Map.Map MajorProtocolVersion (Set.Set Version)
plcVersionsIntroducedIn :: Map.Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set.Set Version)
plcVersionsIntroducedIn =
\case
PlutusV1 ->
Map.fromList
[ (alonzoPV, Set.fromList [ plcVersion100 ])
, (pv11PV, Set.fromList [ plcVersion110 ])
]
PlutusV2 ->
Map.fromList
[ (alonzoPV, Set.fromList [ plcVersion100 ])
, (pv11PV, Set.fromList [ plcVersion110 ])
]
PlutusV3 ->
Map.fromList
[(changPV, Set.fromList [ plcVersion110 ])
]
Map.fromList
[ ((PlutusV1, alonzoPV), Set.fromList [ plcVersion100 ])
, ((PlutusV1, pv11PV), Set.fromList [ plcVersion110 ])
, ((PlutusV2, alonzoPV), Set.fromList [ plcVersion100 ])
, ((PlutusV2, pv11PV), Set.fromList [ plcVersion110 ])
, ((PlutusV3, changPV), Set.fromList [ plcVersion110 ])
]

{-| Which Plutus Core language versions are available in the given 'PlutusLedgerLanguage'
and 'MajorProtocolVersion'? -}
plcVersionsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set.Set Version
plcVersionsAvailableIn = collectUpTo . plcVersionsIntroducedIn
plcVersionsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> (Set.Set Version)
plcVersionsAvailableIn = collectUpTo plcVersionsIntroducedIn