Skip to content

Commit

Permalink
Remove annotation from Version (IntersectMBO#5148)
Browse files Browse the repository at this point in the history
It's not useful, we don't need a position for it or any other kind of
annotation really.
  • Loading branch information
michaelpj authored Feb 15, 2023
1 parent d30867e commit 48c326a
Show file tree
Hide file tree
Showing 22 changed files with 96 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Changed

- `Version` no longer has an annotation, as this was entirely unused.

6 changes: 3 additions & 3 deletions plutus-core/executables/src/PlutusCore/Executable/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -521,15 +521,15 @@ prettyExample =
\case
SomeTypedExample (SomeTypeExample (TypeExample _ ty)) -> PP.prettyPlcDef ty
SomeTypedExample (SomeTypedTermExample (TypedTermExample _ term)) ->
PP.prettyPlcDef $ PLC.Program () (PLC.defaultVersion ()) term
PP.prettyPlcDef $ PLC.Program () (PLC.defaultVersion) term
SomeUntypedExample (SomeUntypedTermExample (UntypedTermExample term)) ->
PP.prettyPlcDef $ UPLC.Program () (PLC.defaultVersion ()) term
PP.prettyPlcDef $ UPLC.Program () (PLC.defaultVersion) term

toTypedTermExample ::
PLC.Term PLC.TyName PLC.Name PLC.DefaultUni PLC.DefaultFun () -> TypedTermExample
toTypedTermExample term = TypedTermExample ty term
where
program = PLC.Program () (PLC.defaultVersion ()) term
program = PLC.Program () (PLC.defaultVersion) term
errOrTy = PLC.runQuote . runExceptT $ do
tcConfig <- PLC.getDefTypeCheckConfig ()
PLC.inferTypeOfProgram tcConfig program
Expand Down
1 change: 1 addition & 0 deletions plutus-core/plutus-core.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ library
PlutusCore.Subst
PlutusCore.TypeCheck
PlutusCore.TypeCheck.Internal
PlutusCore.Version
PlutusIR
PlutusIR.Analysis.RetainedSize
PlutusIR.Compiler
Expand Down
2 changes: 1 addition & 1 deletion plutus-core/plutus-core/src/PlutusCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,4 @@ applyProgram
-- TODO: 'mappend' annotations, ignore versions and return the default one (whatever that means),
-- what a mess. Needs to be fixed.
applyProgram (Program a1 _ t1) (Program a2 _ t2) =
Program (a1 <> a2) (defaultVersion mempty) (Apply mempty t1 t2)
Program (a1 <> a2) defaultVersion (Apply mempty t1 t2)
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ import PlutusPrelude

import PlutusCore.Core.Type

instance Pretty (Version ann) where
pretty (Version _ i j k) = pretty i <> "." <> pretty j <> "." <> pretty k
instance Pretty Version where
pretty (Version i j k) = pretty i <> "." <> pretty j <> "." <> pretty k
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ instance (tyname ~ TyName, name ~ Name) => EstablishScoping (Term tyname name un

instance (tyname ~ TyName, name ~ Name) => EstablishScoping (Program tyname name uni fun) where
establishScoping (Program _ ver term) =
Program NotAName (NotAName <$ ver) <$> establishScoping term
Program NotAName ver <$> establishScoping term

instance tyname ~ TyName => CollectScopeInfo (Type tyname uni) where
collectScopeInfo (TyLam ann name kind ty) =
Expand Down
36 changes: 3 additions & 33 deletions plutus-core/plutus-core/src/PlutusCore/Core/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ module PlutusCore.Core.Type
, argsFunKind
, Type (..)
, Term (..)
, Version (..)
, Program (..)
, UniOf
, Normalized (..)
Expand All @@ -33,7 +32,7 @@ module PlutusCore.Core.Type
, tyDeclVar
, HasUniques
, Binder (..)
, defaultVersion
, module Export
-- * Helper functions
, termAnn
, typeAnn
Expand All @@ -57,6 +56,7 @@ import PlutusPrelude

import PlutusCore.Evaluation.Machine.ExMemory
import PlutusCore.Name
import PlutusCore.Version as Export

import Control.Lens
import Data.Hashable
Expand Down Expand Up @@ -120,36 +120,10 @@ data Term tyname name uni fun ann
instance ExMemoryUsage (Term tyname name uni fun ann) where
memoryUsage = error "Internal error: 'memoryUsage' for Core 'Term' is not supposed to be forced"

{- |
The version of Plutus Core used by this program.
The intention is to convey different levels of backwards compatibility for existing scripts:
- Major version changes are backwards-incompatible
- Minor version changes are backwards-compatible
- Patch version changes should be entirely invisible (and we will likely not use this level)
The version used should be changed only when the /language itself/ changes.
For example, adding a new kind of term to the language would require a minor
version bump; removing a kind of term would require a major version bump.
Similarly, changing the semantics of the language will require a version bump,
typically a major one. This is the main reason why the version is actually
tracked in the AST: we can have two language versions with identical ASTs but
different semantics, so we need to track the version explicitly.
Compatibility is about compatibility for specific scripts, not about e.g. tools which consume scripts.
Adding a new kind of term does not change how existing scripts behave, but does change what
tools would need to do to process scripts.
-}
data Version ann
= Version ann Natural Natural Natural
deriving stock (Eq, Show, Functor, Generic)
deriving anyclass (NFData, Hashable)

-- | A 'Program' is simply a 'Term' coupled with a 'Version' of the core language.
data Program tyname name uni fun ann = Program
{ _progAnn :: ann
, _progVer :: Version ann
, _progVer :: Version
, _progTerm :: Term tyname name uni fun ann
}
deriving stock (Show, Functor, Generic)
Expand Down Expand Up @@ -211,10 +185,6 @@ type instance HasUniques (Term tyname name uni fun ann) =
type instance HasUniques (Program tyname name uni fun ann) =
HasUniques (Term tyname name uni fun ann)

-- | The default version of Plutus Core supported by this library.
defaultVersion :: ann -> Version ann
defaultVersion ann = Version ann 1 0 0

typeAnn :: Type tyname uni ann -> ann
typeAnn (TyVar ann _ ) = ann
typeAnn (TyFun ann _ _ ) = ann
Expand Down
6 changes: 3 additions & 3 deletions plutus-core/plutus-core/src/PlutusCore/Flat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ instance Flat Name where

deriving newtype instance Flat TyName -- via Name

instance Flat ann => Flat (Version ann) where
encode (Version ann n n' n'') = encode ann <> encode n <> encode n' <> encode n''
decode = Version <$> decode <*> decode <*> decode <*> decode
instance Flat Version where
encode (Version n n' n'') = encode n <> encode n' <> encode n''
decode = Version <$> decode <*> decode <*> decode

-- | Use 1 bit to encode kind tags.
kindTagWidth :: NumBits
Expand Down
13 changes: 2 additions & 11 deletions plutus-core/plutus-core/src/PlutusCore/Parser/ParserCommon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,22 +144,13 @@ toSrcSpan start end =
, srcSpanECol = unPos (sourceColumn end)
}

version :: Parser (Version SourcePos)
version :: Parser Version
version = lexeme $ do
p <- getSourcePos
x <- Lex.decimal
void $ char '.'
y <- Lex.decimal
void $ char '.'
Version p x y <$> Lex.decimal

version' :: Parser (Version SrcSpan)
version' = withSpan $ \sp -> do
x <- Lex.decimal
void $ char '.'
y <- Lex.decimal
void $ char '.'
Version sp x y <$> Lex.decimal
Version x y <$> Lex.decimal

name :: Parser Name
name = lexeme $ try $ do
Expand Down
53 changes: 53 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Version.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE TemplateHaskell #-}

module PlutusCore.Version (
Version(..)
, versionMajor
, versionMinor
, versionPatch
, defaultVersion) where

import PlutusPrelude

import Control.Lens
import Data.Hashable
import Instances.TH.Lift ()

{- |
The version of Plutus Core used by this program.
The intention is to convey different levels of backwards compatibility for existing scripts:
- Major version changes are backwards-incompatible
- Minor version changes are backwards-compatible
- Patch version changes should be entirely invisible (and we will likely not use this level)
The version used should be changed only when the /language itself/ changes.
For example, adding a new kind of term to the language would require a minor
version bump; removing a kind of term would require a major version bump.
Similarly, changing the semantics of the language will require a version bump,
typically a major one. This is the main reason why the version is actually
tracked in the AST: we can have two language versions with identical ASTs but
different semantics, so we need to track the version explicitly.
Compatibility is about compatibility for specific scripts, not about e.g. tools which consume
scripts. Adding a new kind of term does not change how existing scripts behave, but does
change what tools would need to do to process scripts.
-}
data Version
= Version { _versionMajor :: Natural, _versionMinor :: Natural, _versionPatch :: Natural }
deriving stock (Eq, Show, Generic)
deriving anyclass (NFData, Hashable)

makeLenses ''Version

-- This is probably what the derived version would do, but better to be explicit since it's
-- important
instance Ord Version where
compare (Version major1 minor1 patch1) (Version major2 minor2 patch2) =
compare major1 major2 <> compare minor1 minor2 <> compare patch1 patch2

-- | The default version of Plutus Core supported by this library.
defaultVersion :: Version
defaultVersion = Version 1 0 0
4 changes: 2 additions & 2 deletions plutus-core/testlib/PlutusCore/Generators/Hedgehog/AST.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ runAstGen a = do
names <- genNames
Gen.fromGenT $ hoist (return . flip runReader names) a

genVersion :: MonadGen m => m (Version ())
genVersion = Version () <$> int' <*> int' <*> int' where
genVersion :: MonadGen m => m Version
genVersion = Version <$> int' <*> int' <*> int' where
int' = Gen.integral_ $ Range.linear 0 10

-- | Generate a fixed set of names which we will use, of only up to a short size to make it
Expand Down
2 changes: 1 addition & 1 deletion plutus-core/testlib/PlutusCore/Generators/Hedgehog/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ getSampleProgramAndValue
=> TermGen a -> IO (Program TyName Name uni fun (), EvaluationResult (Term TyName Name uni fun ()))
getSampleProgramAndValue genTerm =
getSampleTermValue genTerm <&> \(TermOf term result) ->
(Program () (defaultVersion ()) term, result)
(Program () (defaultVersion) term, result)

-- | Generate a program using a given generator, check that it's well-typed and evaluates correctly
-- and pretty-print it to stdout using the default pretty-printing mode.
Expand Down
2 changes: 1 addition & 1 deletion plutus-core/testlib/PlutusIR/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ instance ( PLC.Pretty (PLC.SomeTypeIn uni), PLC.GEq uni, PLC.Typecheckable uni f
, PLC.Closed uni, uni `PLC.Everywhere` PrettyConst, Pretty fun, Pretty a
, Typeable a, Ord a
) => ToTPlc (PIR.Term TyName Name uni fun a) uni fun where
toTPlc = asIfThrown . fmap (PLC.Program () (PLC.defaultVersion ()) . void) . compileAndMaybeTypecheck True
toTPlc = asIfThrown . fmap (PLC.Program () (PLC.defaultVersion) . void) . compileAndMaybeTypecheck True

instance ( PLC.Pretty (PLC.SomeTypeIn uni), PLC.GEq uni, PLC.Typecheckable uni fun
, PLC.Closed uni, uni `PLC.Everywhere` PrettyConst, Pretty fun, Pretty a
Expand Down
4 changes: 2 additions & 2 deletions plutus-core/untyped-plutus-core/src/UntypedPlutusCore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ applyProgram ::
Program name uni fun a ->
Program name uni fun a
applyProgram (Program a1 _ t1) (Program a2 _ t2) =
Program (a1 <> a2) (PLC.defaultVersion mempty) (Apply (termAnn t1 <> termAnn t2) t1 t2)
Program (a1 <> a2) PLC.defaultVersion (Apply (termAnn t1 <> termAnn t2) t1 t2)

{- | DON'T USE, we'll be getting rid of `defaultVersion`.
Turn a UPLC term to a UPLC program with the default version.
-}
mkDefaultProg :: Term name uni fun () -> Program name uni fun ()
mkDefaultProg = Program () (PLC.defaultVersion ())
mkDefaultProg = Program () PLC.defaultVersion
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ data Term name uni fun ann
-- | A 'Program' is simply a 'Term' coupled with a 'Version' of the core language.
data Program name uni fun ann = Program
{ _progAnn :: ann
, _progVer :: TPLC.Version ann
, _progVer :: TPLC.Version
, _progTerm :: Term name uni fun ann
}
deriving stock (Show, Functor, Generic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ program :: Parser (UPLC.Program PLC.Name PLC.DefaultUni PLC.DefaultFun SrcSpan)
program = do
whitespace
prog <- withSpan $ \sp ->
inParens' $ UPLC.Program sp <$> (symbol "program" *> version') <*> term
inParens' $ UPLC.Program sp <$> (symbol "program" *> version) <*> term
notFollowedBy anySingle
pure prog

Expand Down
4 changes: 2 additions & 2 deletions plutus-ledger-api/src/PlutusLedgerApi/Common/Eval.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ data EvaluationError =
CekError !(UPLC.CekEvaluationException NamedDeBruijn DefaultUni DefaultFun) -- ^ An error from the evaluator itself
| DeBruijnError !FreeVariableError -- ^ An error in the pre-evaluation step of converting from de-Bruijn indices
| CodecError !ScriptDecodeError -- ^ A deserialisation error
| IncompatibleVersionError !(ScriptPlutus.Version ()) -- ^ An error indicating a version tag that we don't support
| IncompatibleVersionError !ScriptPlutus.Version -- ^ An error indicating a version tag that we don't support
-- TODO: make this error more informative when we have more information about what went wrong
| CostModelParameterMismatch -- ^ An error indicating that the cost model parameters didn't match what we expected
deriving stock (Show, Eq)
Expand Down Expand Up @@ -95,7 +95,7 @@ mkTermToEvaluate
mkTermToEvaluate lv pv bs args = do
-- It decodes the program through the optimized ScriptForExecution. See `ScriptForExecution`.
ScriptForExecution (UPLC.Program _ v t) <- fromSerialisedScript lv pv bs
unless (v == ScriptPlutus.defaultVersion ()) $ throwError $ IncompatibleVersionError v
unless (v == ScriptPlutus.defaultVersion) $ throwError $ IncompatibleVersionError v
let termArgs = fmap (UPLC.mkConstant ()) args
appliedT = UPLC.mkIterApp () t termArgs

Expand Down
4 changes: 2 additions & 2 deletions plutus-ledger-api/test/Spec/Versions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import Test.Tasty.QuickCheck
serialiseDataExScript :: SerialisedScript
serialiseDataExScript = serialiseUPLC serialiseDataEx
where
serialiseDataEx = UPLC.Program () (PLC.defaultVersion ()) $
serialiseDataEx = UPLC.Program () (PLC.defaultVersion) $
UPLC.Apply () (UPLC.Builtin () PLC.SerialiseData) (PLC.mkConstant () $ I 1)

errorScript :: SerialisedScript
errorScript = serialiseUPLC errorEx
where
errorEx = UPLC.Program () (PLC.defaultVersion ()) $ UPLC.Error ()
errorEx = UPLC.Program () (PLC.defaultVersion) $ UPLC.Error ()

tests :: TestTree
tests = testGroup "versions"
Expand Down
6 changes: 3 additions & 3 deletions plutus-ledger-api/testlib/PlutusLedgerApi/Test/Examples.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ It seems better therefore to avoid depending on Plutus Tx in any "core" projects

-- | Creates a script which has N arguments, and always succeeds.
alwaysSucceedingNAryFunction :: Natural -> SerialisedScript
alwaysSucceedingNAryFunction n = serialiseUPLC $ UPLC.Program () (PLC.defaultVersion ()) (body n)
alwaysSucceedingNAryFunction n = serialiseUPLC $ UPLC.Program () PLC.defaultVersion (body n)
where
-- No more arguments! The body can be anything that doesn't fail, so we return `\x . x`
body i | i == 0 = UPLC.LamAbs() (UPLC.DeBruijn 0) $ UPLC.Var () (UPLC.DeBruijn 1)
Expand All @@ -28,15 +28,15 @@ alwaysSucceedingNAryFunction n = serialiseUPLC $ UPLC.Program () (PLC.defaultVer

-- | Creates a script which has N arguments, and always fails.
alwaysFailingNAryFunction :: Natural -> SerialisedScript
alwaysFailingNAryFunction n = serialiseUPLC $ UPLC.Program () (PLC.defaultVersion ()) (body n)
alwaysFailingNAryFunction n = serialiseUPLC $ UPLC.Program () PLC.defaultVersion (body n)
where
-- No more arguments! The body should be error.
body i | i == 0 = UPLC.Error ()
-- We're using de Bruijn indices, so we can use the same binder each time!
body i = UPLC.LamAbs () (UPLC.DeBruijn 0) $ body (i-1)

summingFunction :: SerialisedScript
summingFunction = serialiseUPLC $ UPLC.Program () (PLC.defaultVersion ()) body
summingFunction = serialiseUPLC $ UPLC.Program () PLC.defaultVersion body
where
body = UPLC.Apply () (UPLC.Apply () (UPLC.Builtin () PLC.AddInteger) (PLC.mkConstant @Integer () 1)) (PLC.mkConstant @Integer () 2)

Expand Down
6 changes: 3 additions & 3 deletions plutus-tx-plugin/src/PlutusTx/Plugin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ runCompiler moduleName opts expr = do

-- (Simplified) Pir -> Plc translation.
plcT <- flip runReaderT pirCtx $ PIR.compileReadableToPlc spirT
let plcP = PLC.Program () (PLC.defaultVersion ()) $ void plcT
let plcP = PLC.Program () (PLC.defaultVersion) $ void plcT
when (_posDumpPlc opts) . liftIO $ dumpFlat plcP "typed PLC program" (moduleName ++ ".plc.flat")

-- We do this after dumping the programs so that if we fail typechecking we still get the dump.
Expand All @@ -431,8 +431,8 @@ runCompiler moduleName opts expr = do

uplcT <- flip runReaderT plcOpts $ PLC.compileTerm plcT
dbT <- liftExcept $ UPLC.deBruijnTerm uplcT
let uplcPNoAnn = UPLC.Program () (PLC.defaultVersion ()) $ void dbT
uplcP = UPLC.Program mempty (PLC.defaultVersion mempty) . fmap getSrcSpans $ dbT
let uplcPNoAnn = UPLC.Program () (PLC.defaultVersion) $ void dbT
uplcP = UPLC.Program mempty (PLC.defaultVersion) . fmap getSrcSpans $ dbT
when (_posDumpUPlc opts) . liftIO $ dumpFlat uplcPNoAnn "untyped PLC program" (moduleName ++ ".uplc.flat")
pure (spirP, uplcP)

Expand Down
6 changes: 3 additions & 3 deletions plutus-tx-plugin/test/IsData/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ tests = testNested "IsData" [
, goldenUEval "tuple" [plc (Proxy @"tuple") (isDataRoundtrip (1::Integer, 2::Integer))]
, goldenUEval "tupleInterop" [
getPlcNoAnn (plc (Proxy @"tupleInterop") (\(d :: P.BuiltinData) -> case IsData.fromBuiltinData d of { Just t -> t P.== (1::Integer, 2::Integer); Nothing -> False}))
, UPLC.Program () (PLC.defaultVersion ()) (PLC.mkConstant () (IsData.toData (1::Integer, 2::Integer)))]
, UPLC.Program () (PLC.defaultVersion) (PLC.mkConstant () (IsData.toData (1::Integer, 2::Integer)))]
, goldenUEval "unsafeTupleInterop" [
getPlcNoAnn (plc (Proxy @"unsafeTupleInterop") (\(d :: P.BuiltinData) -> IsData.unsafeFromBuiltinData d P.== (1::Integer, 2::Integer)))
, UPLC.Program () (PLC.defaultVersion ()) (PLC.mkConstant () (IsData.toData (1::Integer, 2::Integer)))]
, UPLC.Program () (PLC.defaultVersion) (PLC.mkConstant () (IsData.toData (1::Integer, 2::Integer)))]
, goldenUEval "unit" [plc (Proxy @"unit") (isDataRoundtrip ())]
, goldenUEval "unitInterop" [
getPlcNoAnn (plc (Proxy @"unitInterop") (\(d :: P.BuiltinData) -> case IsData.fromBuiltinData d of { Just t -> t P.== (); Nothing -> False}))
, UPLC.Program () (PLC.defaultVersion ()) (PLC.mkConstant () (IsData.toData ()))]
, UPLC.Program () (PLC.defaultVersion) (PLC.mkConstant () (IsData.toData ()))]
, goldenUEval "mono" [plc (Proxy @"mono") (isDataRoundtrip (Mono2 2))]
, goldenUEval "poly" [plc (Proxy @"poly") (isDataRoundtrip (Poly1 (1::Integer) (2::Integer)))]
, goldenUEval "record" [plc (Proxy @"record") (isDataRoundtrip (MyMonoRecord 1 2))]
Expand Down
4 changes: 2 additions & 2 deletions plutus-tx/src/PlutusTx/Lift.hs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ safeLiftProgram
, PrettyPrintable uni fun
)
=> a -> m (UPLC.Program UPLC.NamedDeBruijn uni fun ())
safeLiftProgram x = UPLC.Program () (PLC.defaultVersion ()) <$> safeLift x
safeLiftProgram x = UPLC.Program () (PLC.defaultVersion) <$> safeLift x

safeLiftCode
:: (Lift.Lift uni a
Expand Down Expand Up @@ -128,7 +128,7 @@ lift a = unsafely $ safeLift a
liftProgram
:: (Lift.Lift uni a, Throwable uni fun, PLC.Typecheckable uni fun)
=> a -> UPLC.Program UPLC.NamedDeBruijn uni fun ()
liftProgram x = UPLC.Program () (PLC.defaultVersion ()) $ lift x
liftProgram x = UPLC.Program () (PLC.defaultVersion) $ lift x

-- | Get a Plutus Core program in the default universe corresponding to the given value, throwing any errors that occur as exceptions and ignoring fresh names.
liftProgramDef
Expand Down

0 comments on commit 48c326a

Please sign in to comment.