-- editorconfig-checker-disable-file
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE LambdaCase     #-}

{- | This module contains the code for handling the various kinds of version that we care about:

* Protocol versions
* Plutus ledger languages
* Plutus Core language versions
-}
module PlutusLedgerApi.Common.Versions
    ( -- * Cardano Protocol versions
      module PlutusLedgerApi.Common.ProtocolVersions
      -- * Plutus ledger languages
    , PlutusLedgerLanguage (..)
      -- * Plutus Core language versions
    , Version (..)
      -- * Version-testing functions
    , ledgerLanguageIntroducedIn
    , ledgerLanguagesAvailableIn
    , plcVersionsIntroducedIn
    , plcVersionsAvailableIn
    , builtinsIntroducedIn
    , builtinsAvailableIn
    ) where

import PlutusCore
import PlutusLedgerApi.Common.ProtocolVersions
import PlutusPrelude

import Codec.Serialise.Class (Serialise)
import Data.Map qualified as Map
import Data.Set qualified as Set
import NoThunks.Class (NoThunks)
import PlutusCore.Version (plcVersion100, plcVersion110)
import Prettyprinter

{- Note [New builtins/language versions and protocol versions]
When we add a new builtin to the Plutus language, that is a *backwards-compatible* change.
Old scripts will still work (since they don't use the new builtins), we just make some more
scripts possible.

The same is true for new Plutus Core language versions: adding these is also backwards-compatible.

It would be nice, therefore, to get away with just having one definition of the set of builtin
functions/language features. Then the new features will just "work". However, this neglects the fact that
support for the new feature will be added in the *software update* that
brings a new Plutus ledger language, but they should only be usable after the corresponding
*hard fork*. So there is a period of time in which the feature must be present in the software but not
usable, so we need to decide this conditionally based on the protocol version.

To do this we need to:
- Know which protocol version a feature was introduced in.
- Given the protocol version, check a program for features that should not be usable yet.

To simplify our lives, we pervasively make the assumption that after a
feature is introduced in a ledger-language/protocol-version combo, it is present in all
later ledger-languages/protocol-versions.

Note that this doesn't currently handle removals, although it fairly straighforwardly
could do, just by tracking when they were removed.

See also Note [Adding new builtins: protocol versions].
-}

{-| The Plutus ledger language. These are entirely different script languages from the ledger's perspective,
which on our side are interpreted in very similar ways.

It is a simple enumerated datatype (there is no major and minor components as in protocol version)
and the __ordering of constructors__ is essential for deriving Enum,Ord,Bounded.

IMPORTANT: this is different from the Plutus Core language version, `PlutusCore.Version`
-}
data PlutusLedgerLanguage =
      PlutusV1 -- ^ introduced in shelley era
    | PlutusV2 -- ^ introduced in vasil era
    | PlutusV3 -- ^ not yet enabled
   deriving stock (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
(PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> Eq PlutusLedgerLanguage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
== :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
$c/= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
/= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
Eq, Eq PlutusLedgerLanguage
Eq PlutusLedgerLanguage =>
(PlutusLedgerLanguage -> PlutusLedgerLanguage -> Ordering)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool)
-> (PlutusLedgerLanguage
    -> PlutusLedgerLanguage -> PlutusLedgerLanguage)
-> (PlutusLedgerLanguage
    -> PlutusLedgerLanguage -> PlutusLedgerLanguage)
-> Ord PlutusLedgerLanguage
PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
PlutusLedgerLanguage -> PlutusLedgerLanguage -> Ordering
PlutusLedgerLanguage
-> PlutusLedgerLanguage -> PlutusLedgerLanguage
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Ordering
compare :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Ordering
$c< :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
< :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
$c<= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
<= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
$c> :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
> :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
$c>= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
>= :: PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
$cmax :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> PlutusLedgerLanguage
max :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> PlutusLedgerLanguage
$cmin :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> PlutusLedgerLanguage
min :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> PlutusLedgerLanguage
Ord, Int -> PlutusLedgerLanguage -> ShowS
[PlutusLedgerLanguage] -> ShowS
PlutusLedgerLanguage -> String
(Int -> PlutusLedgerLanguage -> ShowS)
-> (PlutusLedgerLanguage -> String)
-> ([PlutusLedgerLanguage] -> ShowS)
-> Show PlutusLedgerLanguage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PlutusLedgerLanguage -> ShowS
showsPrec :: Int -> PlutusLedgerLanguage -> ShowS
$cshow :: PlutusLedgerLanguage -> String
show :: PlutusLedgerLanguage -> String
$cshowList :: [PlutusLedgerLanguage] -> ShowS
showList :: [PlutusLedgerLanguage] -> ShowS
Show, (forall x. PlutusLedgerLanguage -> Rep PlutusLedgerLanguage x)
-> (forall x. Rep PlutusLedgerLanguage x -> PlutusLedgerLanguage)
-> Generic PlutusLedgerLanguage
forall x. Rep PlutusLedgerLanguage x -> PlutusLedgerLanguage
forall x. PlutusLedgerLanguage -> Rep PlutusLedgerLanguage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PlutusLedgerLanguage -> Rep PlutusLedgerLanguage x
from :: forall x. PlutusLedgerLanguage -> Rep PlutusLedgerLanguage x
$cto :: forall x. Rep PlutusLedgerLanguage x -> PlutusLedgerLanguage
to :: forall x. Rep PlutusLedgerLanguage x -> PlutusLedgerLanguage
Generic, Int -> PlutusLedgerLanguage
PlutusLedgerLanguage -> Int
PlutusLedgerLanguage -> [PlutusLedgerLanguage]
PlutusLedgerLanguage -> PlutusLedgerLanguage
PlutusLedgerLanguage
-> PlutusLedgerLanguage -> [PlutusLedgerLanguage]
PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> [PlutusLedgerLanguage]
(PlutusLedgerLanguage -> PlutusLedgerLanguage)
-> (PlutusLedgerLanguage -> PlutusLedgerLanguage)
-> (Int -> PlutusLedgerLanguage)
-> (PlutusLedgerLanguage -> Int)
-> (PlutusLedgerLanguage -> [PlutusLedgerLanguage])
-> (PlutusLedgerLanguage
    -> PlutusLedgerLanguage -> [PlutusLedgerLanguage])
-> (PlutusLedgerLanguage
    -> PlutusLedgerLanguage -> [PlutusLedgerLanguage])
-> (PlutusLedgerLanguage
    -> PlutusLedgerLanguage
    -> PlutusLedgerLanguage
    -> [PlutusLedgerLanguage])
-> Enum PlutusLedgerLanguage
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: PlutusLedgerLanguage -> PlutusLedgerLanguage
succ :: PlutusLedgerLanguage -> PlutusLedgerLanguage
$cpred :: PlutusLedgerLanguage -> PlutusLedgerLanguage
pred :: PlutusLedgerLanguage -> PlutusLedgerLanguage
$ctoEnum :: Int -> PlutusLedgerLanguage
toEnum :: Int -> PlutusLedgerLanguage
$cfromEnum :: PlutusLedgerLanguage -> Int
fromEnum :: PlutusLedgerLanguage -> Int
$cenumFrom :: PlutusLedgerLanguage -> [PlutusLedgerLanguage]
enumFrom :: PlutusLedgerLanguage -> [PlutusLedgerLanguage]
$cenumFromThen :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> [PlutusLedgerLanguage]
enumFromThen :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> [PlutusLedgerLanguage]
$cenumFromTo :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> [PlutusLedgerLanguage]
enumFromTo :: PlutusLedgerLanguage
-> PlutusLedgerLanguage -> [PlutusLedgerLanguage]
$cenumFromThenTo :: PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> [PlutusLedgerLanguage]
enumFromThenTo :: PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> PlutusLedgerLanguage
-> [PlutusLedgerLanguage]
Enum, PlutusLedgerLanguage
PlutusLedgerLanguage
-> PlutusLedgerLanguage -> Bounded PlutusLedgerLanguage
forall a. a -> a -> Bounded a
$cminBound :: PlutusLedgerLanguage
minBound :: PlutusLedgerLanguage
$cmaxBound :: PlutusLedgerLanguage
maxBound :: PlutusLedgerLanguage
Bounded)
   deriving anyclass (PlutusLedgerLanguage -> ()
(PlutusLedgerLanguage -> ()) -> NFData PlutusLedgerLanguage
forall a. (a -> ()) -> NFData a
$crnf :: PlutusLedgerLanguage -> ()
rnf :: PlutusLedgerLanguage -> ()
NFData, Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo)
Proxy PlutusLedgerLanguage -> String
(Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo))
-> (Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo))
-> (Proxy PlutusLedgerLanguage -> String)
-> NoThunks PlutusLedgerLanguage
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
$cnoThunks :: Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo)
noThunks :: Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> PlutusLedgerLanguage -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy PlutusLedgerLanguage -> String
showTypeOf :: Proxy PlutusLedgerLanguage -> String
NoThunks, [PlutusLedgerLanguage] -> Encoding
PlutusLedgerLanguage -> Encoding
(PlutusLedgerLanguage -> Encoding)
-> (forall s. Decoder s PlutusLedgerLanguage)
-> ([PlutusLedgerLanguage] -> Encoding)
-> (forall s. Decoder s [PlutusLedgerLanguage])
-> Serialise PlutusLedgerLanguage
forall s. Decoder s [PlutusLedgerLanguage]
forall s. Decoder s PlutusLedgerLanguage
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
$cencode :: PlutusLedgerLanguage -> Encoding
encode :: PlutusLedgerLanguage -> Encoding
$cdecode :: forall s. Decoder s PlutusLedgerLanguage
decode :: forall s. Decoder s PlutusLedgerLanguage
$cencodeList :: [PlutusLedgerLanguage] -> Encoding
encodeList :: [PlutusLedgerLanguage] -> Encoding
$cdecodeList :: forall s. Decoder s [PlutusLedgerLanguage]
decodeList :: forall s. Decoder s [PlutusLedgerLanguage]
Serialise)

instance Pretty PlutusLedgerLanguage where
    pretty :: forall ann. PlutusLedgerLanguage -> Doc ann
pretty = PlutusLedgerLanguage -> Doc ann
forall a ann. Show a => a -> Doc ann
viaShow

{-| A map indicating which builtin functions were introduced in which 'MajorProtocolVersion'.

This __must__ be updated when new builtins are added.
See Note [New builtins/language versions and protocol versions]
-}
builtinsIntroducedIn :: Map.Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set.Set DefaultFun)
builtinsIntroducedIn :: Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
builtinsIntroducedIn = [((PlutusLedgerLanguage, MajorProtocolVersion), Set DefaultFun)]
-> Map
     (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [
  ((PlutusLedgerLanguage
PlutusV1, MajorProtocolVersion
alonzoPV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
AddInteger, DefaultFun
SubtractInteger, DefaultFun
MultiplyInteger, DefaultFun
DivideInteger, DefaultFun
QuotientInteger, DefaultFun
RemainderInteger, DefaultFun
ModInteger, DefaultFun
EqualsInteger, DefaultFun
LessThanInteger, DefaultFun
LessThanEqualsInteger,
          DefaultFun
AppendByteString, DefaultFun
ConsByteString, DefaultFun
SliceByteString, DefaultFun
LengthOfByteString, DefaultFun
IndexByteString, DefaultFun
EqualsByteString, DefaultFun
LessThanByteString, DefaultFun
LessThanEqualsByteString,
          DefaultFun
Sha2_256, DefaultFun
Sha3_256, DefaultFun
Blake2b_256, DefaultFun
VerifyEd25519Signature,
          DefaultFun
AppendString, DefaultFun
EqualsString, DefaultFun
EncodeUtf8, DefaultFun
DecodeUtf8,
          DefaultFun
IfThenElse,
          DefaultFun
ChooseUnit,
          DefaultFun
Trace,
          DefaultFun
FstPair, DefaultFun
SndPair,
          DefaultFun
ChooseList, DefaultFun
MkCons, DefaultFun
HeadList, DefaultFun
TailList, DefaultFun
NullList,
          DefaultFun
ChooseData, DefaultFun
ConstrData, DefaultFun
MapData, DefaultFun
ListData, DefaultFun
IData, DefaultFun
BData, DefaultFun
UnConstrData, DefaultFun
UnMapData, DefaultFun
UnListData, DefaultFun
UnIData, DefaultFun
UnBData, DefaultFun
EqualsData,
          DefaultFun
MkPairData, DefaultFun
MkNilData, DefaultFun
MkNilPairData
          ]),
  ((PlutusLedgerLanguage
PlutusV2, MajorProtocolVersion
vasilPV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
SerialiseData
          ]),
  ((PlutusLedgerLanguage
PlutusV2, MajorProtocolVersion
valentinePV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
VerifyEcdsaSecp256k1Signature, DefaultFun
VerifySchnorrSecp256k1Signature
          ]),
  ((PlutusLedgerLanguage
PlutusV2, MajorProtocolVersion
changPlus1PV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
IntegerToByteString, DefaultFun
ByteStringToInteger
          ]),
  ((PlutusLedgerLanguage
PlutusV3, MajorProtocolVersion
changPV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
Bls12_381_G1_add, DefaultFun
Bls12_381_G1_neg, DefaultFun
Bls12_381_G1_scalarMul,
          DefaultFun
Bls12_381_G1_equal, DefaultFun
Bls12_381_G1_hashToGroup,
          DefaultFun
Bls12_381_G1_compress, DefaultFun
Bls12_381_G1_uncompress,
          DefaultFun
Bls12_381_G2_add, DefaultFun
Bls12_381_G2_neg, DefaultFun
Bls12_381_G2_scalarMul,
          DefaultFun
Bls12_381_G2_equal, DefaultFun
Bls12_381_G2_hashToGroup,
          DefaultFun
Bls12_381_G2_compress, DefaultFun
Bls12_381_G2_uncompress,
          DefaultFun
Bls12_381_millerLoop, DefaultFun
Bls12_381_mulMlResult, DefaultFun
Bls12_381_finalVerify,
          DefaultFun
Keccak_256, DefaultFun
Blake2b_224, DefaultFun
IntegerToByteString, DefaultFun
ByteStringToInteger
          ]),
  ((PlutusLedgerLanguage
PlutusV3, MajorProtocolVersion
changPlus1PV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
AndByteString, DefaultFun
OrByteString, DefaultFun
XorByteString, DefaultFun
ComplementByteString,
          DefaultFun
ReadBit, DefaultFun
WriteBits, DefaultFun
ReplicateByte,
          DefaultFun
ShiftByteString, DefaultFun
RotateByteString, DefaultFun
CountSetBits, DefaultFun
FindFirstSetBit,
          DefaultFun
Ripemd_160
          ]),
  ((PlutusLedgerLanguage
PlutusV3, MajorProtocolVersion
futurePV), [DefaultFun] -> Set DefaultFun
forall a. Ord a => [a] -> Set a
Set.fromList [
          DefaultFun
ExpModInteger
          ])
  ]

{-| 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 :: Map.Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set.Set Version)
plcVersionsIntroducedIn :: Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
plcVersionsIntroducedIn = [((PlutusLedgerLanguage, MajorProtocolVersion), Set Version)]
-> Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [
  ((PlutusLedgerLanguage
PlutusV1, MajorProtocolVersion
alonzoPV), [Version] -> Set Version
forall a. Ord a => [a] -> Set a
Set.fromList [ Version
plcVersion100 ]),
  ((PlutusLedgerLanguage
PlutusV3, MajorProtocolVersion
changPV), [Version] -> Set Version
forall a. Ord a => [a] -> Set a
Set.fromList [ Version
plcVersion110 ])
  ]

{-| Query the protocol version that a specific Plutus ledger language was first introduced in.
-}
ledgerLanguageIntroducedIn :: PlutusLedgerLanguage -> MajorProtocolVersion
ledgerLanguageIntroducedIn :: PlutusLedgerLanguage -> MajorProtocolVersion
ledgerLanguageIntroducedIn = \case
    PlutusLedgerLanguage
PlutusV1 -> MajorProtocolVersion
alonzoPV
    PlutusLedgerLanguage
PlutusV2 -> MajorProtocolVersion
vasilPV
    PlutusLedgerLanguage
PlutusV3 -> MajorProtocolVersion
changPV

{-| Which Plutus language versions are available in the given 'MajorProtocolVersion'?

See Note [New builtins/language versions and protocol versions]
-}
ledgerLanguagesAvailableIn :: MajorProtocolVersion -> Set.Set PlutusLedgerLanguage
ledgerLanguagesAvailableIn :: MajorProtocolVersion -> Set PlutusLedgerLanguage
ledgerLanguagesAvailableIn MajorProtocolVersion
searchPv =
    (PlutusLedgerLanguage -> Set PlutusLedgerLanguage)
-> [PlutusLedgerLanguage] -> Set PlutusLedgerLanguage
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap PlutusLedgerLanguage -> Set PlutusLedgerLanguage
ledgerVersionToSet [PlutusLedgerLanguage]
forall a. (Enum a, Bounded a) => [a]
enumerate
  where
    -- OPTIMIZE: could be done faster using takeWhile
    ledgerVersionToSet :: PlutusLedgerLanguage -> Set.Set PlutusLedgerLanguage
    ledgerVersionToSet :: PlutusLedgerLanguage -> Set PlutusLedgerLanguage
ledgerVersionToSet PlutusLedgerLanguage
ll
        | PlutusLedgerLanguage -> MajorProtocolVersion
ledgerLanguageIntroducedIn PlutusLedgerLanguage
ll MajorProtocolVersion -> MajorProtocolVersion -> Bool
forall a. Ord a => a -> a -> Bool
<= MajorProtocolVersion
searchPv = PlutusLedgerLanguage -> Set PlutusLedgerLanguage
forall a. a -> Set a
Set.singleton PlutusLedgerLanguage
ll
        | Bool
otherwise = Set PlutusLedgerLanguage
forall a. Monoid a => a
mempty

{-| Which Plutus Core language versions are available in the given 'PlutusLedgerLanguage'
and 'MajorProtocolVersion'?

See Note [New builtins/language versions and protocol versions]
-}
plcVersionsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set.Set Version
plcVersionsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set Version
plcVersionsAvailableIn PlutusLedgerLanguage
thisLv MajorProtocolVersion
thisPv = [Set Version] -> Set Version
forall m. Monoid m => [m] -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold ([Set Version] -> Set Version) -> [Set Version] -> Set Version
forall a b. (a -> b) -> a -> b
$ Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
-> [Set Version]
forall k a. Map k a -> [a]
Map.elems (Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
 -> [Set Version])
-> Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
-> [Set Version]
forall a b. (a -> b) -> a -> b
$
    ((PlutusLedgerLanguage, MajorProtocolVersion) -> Bool)
-> Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
-> Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
forall k a. (k -> Bool) -> Map k a -> Map k a
Map.takeWhileAntitone (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
plcVersionAvailableIn Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set Version)
plcVersionsIntroducedIn
    where
      plcVersionAvailableIn :: (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
      plcVersionAvailableIn :: (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
plcVersionAvailableIn (PlutusLedgerLanguage
introducedInLv,MajorProtocolVersion
introducedInPv) =
          -- both should be satisfied
          PlutusLedgerLanguage
introducedInLv PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
forall a. Ord a => a -> a -> Bool
<= PlutusLedgerLanguage
thisLv Bool -> Bool -> Bool
&& MajorProtocolVersion
introducedInPv MajorProtocolVersion -> MajorProtocolVersion -> Bool
forall a. Ord a => a -> a -> Bool
<= MajorProtocolVersion
thisPv

{-| Which builtin functions are available in the given given 'PlutusLedgerLanguage'
and 'MajorProtocolVersion'?

See Note [New builtins/language versions and protocol versions]
-}
builtinsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set.Set DefaultFun
builtinsAvailableIn :: PlutusLedgerLanguage -> MajorProtocolVersion -> Set DefaultFun
builtinsAvailableIn PlutusLedgerLanguage
thisLv MajorProtocolVersion
thisPv = Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
-> Set DefaultFun
forall m.
Monoid m =>
Map (PlutusLedgerLanguage, MajorProtocolVersion) m -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold (Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
 -> Set DefaultFun)
-> Map
     (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
-> Set DefaultFun
forall a b. (a -> b) -> a -> b
$
    ((PlutusLedgerLanguage, MajorProtocolVersion)
 -> Set DefaultFun -> Bool)
-> Map
     (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
-> Map
     (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
forall k a. (k -> a -> Bool) -> Map k a -> Map k a
Map.filterWithKey (Bool -> Set DefaultFun -> Bool
forall a b. a -> b -> a
const (Bool -> Set DefaultFun -> Bool)
-> ((PlutusLedgerLanguage, MajorProtocolVersion) -> Bool)
-> (PlutusLedgerLanguage, MajorProtocolVersion)
-> Set DefaultFun
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
alreadyIntroduced) Map (PlutusLedgerLanguage, MajorProtocolVersion) (Set DefaultFun)
builtinsIntroducedIn
    where
      alreadyIntroduced :: (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
      alreadyIntroduced :: (PlutusLedgerLanguage, MajorProtocolVersion) -> Bool
alreadyIntroduced (PlutusLedgerLanguage
introducedInLv,MajorProtocolVersion
introducedInPv) =
          -- both should be satisfied
          PlutusLedgerLanguage
introducedInLv PlutusLedgerLanguage -> PlutusLedgerLanguage -> Bool
forall a. Ord a => a -> a -> Bool
<= PlutusLedgerLanguage
thisLv Bool -> Bool -> Bool
&& MajorProtocolVersion
introducedInPv MajorProtocolVersion -> MajorProtocolVersion -> Bool
forall a. Ord a => a -> a -> Bool
<= MajorProtocolVersion
thisPv