Skip to content

Commit

Permalink
Fix some issues and inconsistencies related to pretty-printing (#6515)
Browse files Browse the repository at this point in the history
  • Loading branch information
zliu41 authored Sep 27, 2024
1 parent 348c838 commit f912edc
Show file tree
Hide file tree
Showing 137 changed files with 3,120 additions and 3,352 deletions.
12 changes: 12 additions & 0 deletions plutus-core/changelog.d/20240924_105256_unsafeFixIO_pretty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

### Removed

- Removed `PlutusIR.Core.Instance.Pretty.Readable.PrettyPir`.
Use `PlutusCore.Pretty.Readable.PrettyReadable` instead.

### Changed

- Renamed `PlutusIR.Core.Instance.Pretty.Readable.prettyPirReadable`
to `PlutusCore.Pretty.Readable.prettyReadable`.
- Renamed `PlutusIR.Core.Instance.Pretty.Readable.prettyPirReadableSimple`
to `PlutusCore.Pretty.Readable.prettyReadableSimple`.
2 changes: 2 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ module PlutusCore.Pretty
-- * Readable view
, ShowKinds (..)
, PrettyConfigReadable (..)
, prettyReadable
, prettyReadableSimple
, pcrConfigName
, pcrRenderContext
, pcrShowKinds
Expand Down
8 changes: 8 additions & 0 deletions plutus-core/plutus-core/src/PlutusCore/Pretty/Readable.hs
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,11 @@ iterInterAppPrettyM fun args =
let ppArg (Left ty) = prettyArg $ inBraces ty
ppArg (Right term) = prettyArg term
in prettyFun fun :| map ppArg args

-- | Pretty-print something with the @PrettyConfigReadable@ config.
prettyReadable :: (PrettyReadable a) => a -> Doc ann
prettyReadable = prettyBy (botPrettyConfigReadable prettyConfigName def)

-- | Pretty-print something with the @PrettyConfigReadableSimple@ config.
prettyReadableSimple :: (PrettyReadable a) => a -> Doc ann
prettyReadableSimple = prettyBy (botPrettyConfigReadable prettyConfigNameSimple def)
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
{-# OPTIONS_GHC -Wno-unrecognised-pragmas #-}
{-# HLINT ignore "Eta reduce" #-} -- breaks type inference

module PlutusIR.Core.Instance.Pretty.Readable
( prettyPirReadable
, prettyPirReadableSimple
, PrettyPir
) where
module PlutusIR.Core.Instance.Pretty.Readable () where

import PlutusCore.Pretty
import PlutusIR.Core.Type
Expand All @@ -25,16 +21,6 @@ import Data.Profunctor
import Prettyprinter
import Prettyprinter.Custom

type PrettyPir = PrettyBy (PrettyConfigReadable PrettyConfigName)

-- | Pretty-print something with the @PrettyConfigReadable@ config.
prettyPirReadable :: PrettyPir a => a -> Doc ann
prettyPirReadable = prettyBy (botPrettyConfigReadable prettyConfigName def)

-- | Pretty-print something with the @PrettyConfigReadableSimple@ config.
prettyPirReadableSimple :: PrettyPir a => a -> Doc ann
prettyPirReadableSimple = prettyBy (botPrettyConfigReadable prettyConfigNameSimple def)

-- | Split an iterated 'LamAbs' (if any) into a list of variables that it binds and its body.
viewLamAbs
:: Term tyname name uni fun ann
Expand Down
5 changes: 2 additions & 3 deletions plutus-core/plutus-ir/test/PlutusIR/Core/Tests.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module PlutusIR.Core.Tests where

import PlutusCore qualified as PLC
import PlutusCore.Pretty
import PlutusIR
import PlutusIR.Core.Instance.Pretty.Readable
import PlutusIR.Parser (pTerm)
import PlutusIR.Test

Expand All @@ -25,7 +25,7 @@ test_prettyprintingReadable :: TestTree
test_prettyprintingReadable =
runTestNested ["plutus-ir", "test", "PlutusIR", "Core", "prettyprintingReadable"] $
map
(goldenPirDoc prettyPirReadableSimple pTerm)
(goldenPirDoc prettyReadableSimple pTerm)
[ "basic"
, "maybe"
, "letInLet"
Expand Down Expand Up @@ -60,4 +60,3 @@ roundTripPirTerm = decodeOrError . unflat . flat . void
where
decodeOrError (Right tm) = tm
decodeOrError (Left err) = error (show err)

Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import PlutusCore.Default
import PlutusCore.Evaluation.Machine.ExBudget
import PlutusCore.Evaluation.Machine.ExBudgetingDefaults (defaultCekParametersForTesting)
import PlutusCore.Name.Unique
import PlutusCore.Pretty
import PlutusCore.Quote
import PlutusCore.Rename
import PlutusCore.Test (toUPlc)
import PlutusCore.Version (latestVersion)
import PlutusIR
import PlutusIR.Core.Instance.Pretty.Readable
import PlutusIR.Test ()
import UntypedPlutusCore qualified as UPLC
import UntypedPlutusCore.Evaluation.Machine.Cek (restricting, runCekNoEmit,
Expand Down Expand Up @@ -142,7 +142,7 @@ prop_stats_leaves :: Property
prop_stats_leaves = withMaxSuccess 10 $
-- No shrinking here because we are only collecting stats
forAllDoc "_,tm" genTypeAndTerm_ (const []) $ \ (_, tm) ->
tabulate "leaves" (map (filter isAlpha . show . prettyPirReadable) $ leaves tm) $ property True
tabulate "leaves" (map (filter isAlpha . show . prettyReadable) $ leaves tm) $ property True
where
-- Figure out what's at the leaves of the AST,
-- including variable names, error, and builtins.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import PlutusCore.Core
import PlutusCore.Default
import PlutusCore.Name.Unique
import PlutusCore.Normalize
import PlutusCore.Pretty
import PlutusCore.Quote (runQuote)
import PlutusIR
import PlutusIR.Core.Instance.Pretty.Readable

import Control.Monad (replicateM, when)
import Control.Monad.Reader (asks, local)
Expand Down Expand Up @@ -89,9 +89,9 @@ genType k = do
Right _ -> pure ()
Left err ->
error . show $ fold
[ "genType - checkInvariants: type " <> prettyPirReadable ty
, " does not match kind " <> prettyPirReadable k
, " in context " <> prettyPirReadable ctx
[ "genType - checkInvariants: type " <> prettyReadable ty
, " does not match kind " <> prettyReadable k
, " in context " <> prettyReadable ctx
, " with error message " <> fromString err
]
pure ty
Expand Down
13 changes: 6 additions & 7 deletions plutus-core/testlib/PlutusCore/Generators/QuickCheck/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import PlutusCore.Pretty
import PlutusCore.Quote
import PlutusIR
import PlutusIR.Compiler.Datatype
import PlutusIR.Core.Instance.Pretty.Readable
import PlutusIR.Subst

import Data.Kind qualified as GHC
Expand Down Expand Up @@ -43,31 +42,31 @@ ceDoc d = counterexample (render d)
-- | Bind a value to a name in a property so that
-- it is displayed as a `name = thing` binding if the
-- property fails.
letCE :: (PrettyPir a, Testable p)
letCE :: (PrettyReadable a, Testable p)
=> String
-> a
-> (a -> p)
-> Property
letCE name x k = ceDoc (fromString name <+> "=" <+> prettyPirReadable x) (k x)
letCE name x k = ceDoc (fromString name <+> "=" <+> prettyReadable x) (k x)

-- | Like `forAllShrink` but displays the bound value as
-- a named pretty-printed binding like `letCE`
forAllDoc :: (PrettyPir a, Testable p)
forAllDoc :: (PrettyReadable a, Testable p)
=> String
-> Gen a
-> (a -> [a])
-> (a -> p)
-> Property
forAllDoc name g shr k =
forAllShrinkBlind g shr $ \ x ->
ceDoc (fromString name <+> "=" <+> prettyPirReadable x)
ceDoc (fromString name <+> "=" <+> prettyReadable x)
(k x)

-- | Check that a list of potential counterexamples is empty and display the
-- list as a QuickCheck counterexample if its not.
assertNoCounterexamples :: PrettyPir a => [a] -> Property
assertNoCounterexamples :: (PrettyReadable a) => [a] -> Property
assertNoCounterexamples [] = property True
assertNoCounterexamples bad = ceDoc (prettyPirReadable bad) False
assertNoCounterexamples bad = ceDoc (prettyReadable bad) False

-- * Containers (zipper-ish, very useful for shrinking).

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ import PlutusCore.Core (argsFunKind)
import PlutusCore.Default
import PlutusCore.MkPlc (mkConstantOf)
import PlutusCore.Name.Unique
import PlutusCore.Pretty
import PlutusCore.Subst (typeSubstClosedType)
import PlutusIR
import PlutusIR.Compiler
import PlutusIR.Core.Instance.Pretty.Readable
import PlutusIR.Subst

import Control.Lens ((<&>))
Expand All @@ -55,7 +55,6 @@ import Data.Set.Lens (setOf)
import Data.String
import GHC.Stack
import Prettyprinter
import Text.PrettyBy

-- | This type keeps track of what kind of argument, term argument (`InstArg`) or
-- type argument (`InstApp`) is required for a function. This type is used primarily
Expand Down Expand Up @@ -264,10 +263,10 @@ genTerm mty = checkInvariants $ do
if debug then
case typeCheckTermInContext tyctx tmctx tm ty of
Left err ->
(error . show $ "genTerm - checkInvariants: term " <> prettyPirReadable tm
<> " does not type check at type " <> prettyPirReadable ty
<> " in type context " <> prettyPirReadable tyctx
<> " and term context " <> prettyPirReadable tmctx
(error . show $ "genTerm - checkInvariants: term " <> prettyReadable tm
<> " does not type check at type " <> prettyReadable ty
<> " in type context " <> prettyReadable tyctx
<> " and term context " <> prettyReadable tmctx
<> " with error message " <> fromString err)
_ -> return (ty, tm)
else
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
program
1.1.0
(\(x : integer) ->
addInteger
24680135792468013579
\(x : integer) ->
addInteger
24680135792468013579
(addInteger
-99887766554433221100
(addInteger
-99887766554433221100
98765432109876543210
(addInteger
98765432109876543210
-654
(addInteger
-654
456
(addInteger
456
13579246801357924680
(addInteger
13579246801357924680
-11223344556677889900
(addInteger
-11223344556677889900
(addInteger
12345678901234567890
(addInteger -321 (multiplyInteger 123 x))))))))))
12345678901234567890
(addInteger -321 (multiplyInteger 123 x)))))))))
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
program
1.1.0
(\(x : integer) ->
addInteger
24680135792468013579
\(x : integer) ->
addInteger
24680135792468013579
(addInteger
-99887766554433221100
(addInteger
-99887766554433221100
98765432109876543210
(addInteger
98765432109876543210
-654
(addInteger
-654
456
(addInteger
456
13579246801357924680
(addInteger
13579246801357924680
-11223344556677889900
(addInteger
-11223344556677889900
(addInteger
12345678901234567890
(addInteger -321 (multiplyInteger 123 x))))))))))
12345678901234567890
(addInteger -321 (multiplyInteger 123 x)))))))))
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
program
1.1.0
(\(x : integer) ->
addInteger
24680135792468013579
\(x : integer) ->
addInteger
24680135792468013579
(addInteger
-99887766554433221100
(addInteger
-99887766554433221100
98765432109876543210
(addInteger
98765432109876543210
-654
(addInteger
-654
456
(addInteger
456
13579246801357924680
(addInteger
13579246801357924680
-11223344556677889900
(addInteger
-11223344556677889900
(addInteger
12345678901234567890
(addInteger -321 (multiplyInteger 123 x))))))))))
12345678901234567890
(addInteger -321 (multiplyInteger 123 x)))))))))
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
program
1.1.0
(\(x : integer) ->
addInteger
24680135792468013579
\(x : integer) ->
addInteger
24680135792468013579
(addInteger
-99887766554433221100
(addInteger
-99887766554433221100
98765432109876543210
(addInteger
98765432109876543210
-654
(addInteger
-654
456
(addInteger
456
13579246801357924680
(addInteger
13579246801357924680
-11223344556677889900
(addInteger
-11223344556677889900
(addInteger
12345678901234567890
(addInteger -321 (multiplyInteger 123 x))))))))))
12345678901234567890
(addInteger -321 (multiplyInteger 123 x)))))))))
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Test.Tasty.Extras

tests :: TestNested
tests = testNested "IntegerLiterals" . pure $ testNestedGhc
[ goldenPir "integerLiterals-NoStrict-NegativeLiterals" integerLiterals
[ goldenPirReadable "integerLiterals-NoStrict-NegativeLiterals" integerLiterals
]

integerLiterals :: CompiledCode (Integer -> Integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Test.Tasty.Extras

tests :: TestNested
tests = testNested "IntegerLiterals" . pure $ testNestedGhc
[ goldenPir "integerLiterals-NoStrict-NoNegativeLiterals" integerLiterals
[ goldenPirReadable "integerLiterals-NoStrict-NoNegativeLiterals" integerLiterals
]

integerLiterals :: CompiledCode (Integer -> Integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import Test.Tasty.Extras

tests :: TestNested
tests = testNested "IntegerLiterals" . pure $ testNestedGhc
[ goldenPir "integerLiterals-Strict-NegativeLiterals" integerLiterals
[ goldenPirReadable "integerLiterals-Strict-NegativeLiterals" integerLiterals
]

integerLiterals :: CompiledCode (Integer -> Integer)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Test.Tasty.Extras

tests :: TestNested
tests = testNested "IntegerLiterals" . pure $ testNestedGhc
[ goldenPir "integerLiterals-Strict-NoNegativeLiterals" integerLiterals
[ goldenPirReadable "integerLiterals-Strict-NoNegativeLiterals" integerLiterals
]

integerLiterals :: CompiledCode (Integer -> Integer)
Expand Down
Loading

1 comment on commit f912edc

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Plutus Benchmarks'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: f912edc Previous: 348c838 Ratio
validation-auction_1-4 327.5 μs 289 μs 1.13
validation-auction_2-1 257.1 μs 212.4 μs 1.21
validation-escrow-redeem_2-1 580 μs 524.6 μs 1.11
validation-escrow-redeem_2-2 580.1 μs 403.1 μs 1.44
validation-escrow-redeem_2-3 578.5 μs 404.3 μs 1.43
validation-escrow-refund-1 221.7 μs 153.5 μs 1.44
validation-future-increase-margin-1 341.5 μs 244.6 μs 1.40
validation-game-sm-success_1-2 245.5 μs 201 μs 1.22
validation-game-sm-success_1-3 908.9 μs 638.2 μs 1.42
validation-game-sm-success_1-4 332.8 μs 233.3 μs 1.43
validation-game-sm-success_2-1 553.2 μs 388.6 μs 1.42
validation-game-sm-success_2-2 285.2 μs 200.8 μs 1.42
validation-game-sm-success_2-3 911.1 μs 818 μs 1.11
validation-multisig-sm-1 565.3 μs 404.6 μs 1.40
validation-multisig-sm-2 549.4 μs 394.8 μs 1.39
validation-multisig-sm-8 560 μs 513.7 μs 1.09
validation-multisig-sm-9 561.9 μs 392.5 μs 1.43
validation-multisig-sm-10 792.3 μs 554.1 μs 1.43
validation-ping-pong-1 469.7 μs 332.1 μs 1.41
validation-ping-pong-2 469.3 μs 331.8 μs 1.41
validation-ping-pong_2-1 285.3 μs 200.7 μs 1.42
validation-uniswap-4 476.3 μs 333.3 μs 1.43
validation-uniswap-5 1640 μs 1148 μs 1.43
validation-uniswap-6 457.4 μs 319.7 μs 1.43
validation-vesting-1 500 μs 350.3 μs 1.43
validation-decode-auction_1-1 273.4 μs 188.2 μs 1.45
validation-decode-auction_1-2 762.9 μs 539.6 μs 1.41
validation-decode-escrow-redeem_1-2 443.2 μs 308.7 μs 1.44
validation-decode-escrow-redeem_2-1 443.9 μs 308.6 μs 1.44
validation-decode-escrow-redeem_2-2 445 μs 308.3 μs 1.44
validation-decode-escrow-redeem_2-3 332.3 μs 308.9 μs 1.08
validation-decode-escrow-refund-1 444.4 μs 309.3 μs 1.44
validation-decode-future-increase-margin-1 328.8 μs 227.3 μs 1.45
validation-decode-future-increase-margin-2 444.9 μs 325.2 μs 1.37
validation-decode-future-pay-out-3 440.8 μs 409.9 μs 1.08
validation-decode-future-pay-out-4 969.3 μs 917.3 μs 1.06
validation-decode-future-settle-early-1 334.4 μs 318.2 μs 1.05
validation-decode-game-sm-success_1-2 230.2 μs 201.3 μs 1.14
validation-decode-game-sm-success_1-3 730.7 μs 505.5 μs 1.45
validation-decode-game-sm-success_1-4 229.5 μs 165.3 μs 1.39
validation-decode-game-sm-success_2-1 736.9 μs 504.3 μs 1.46
validation-decode-game-sm-success_2-2 200.6 μs 160 μs 1.25
validation-decode-game-sm-success_2-3 543 μs 505.6 μs 1.07
validation-decode-multisig-sm-7 686.5 μs 561.7 μs 1.22
validation-decode-multisig-sm-8 801.6 μs 563.4 μs 1.42
validation-decode-multisig-sm-9 822.6 μs 563.4 μs 1.46
validation-decode-multisig-sm-10 824.5 μs 560.6 μs 1.47
validation-decode-ping-pong-1 694.4 μs 470.8 μs 1.47
validation-decode-ping-pong-2 695.4 μs 599.2 μs 1.16
validation-decode-stablecoin_2-1 910.7 μs 834.3 μs 1.09
validation-decode-stablecoin_2-3 908.1 μs 833.7 μs 1.09
validation-decode-stablecoin_2-4 230.2 μs 159.5 μs 1.44
validation-decode-token-account-1 337.5 μs 225.4 μs 1.50
validation-decode-token-account-2 266.9 μs 207.5 μs 1.29
validation-decode-uniswap-3 838.1 μs 713.8 μs 1.17
validation-decode-uniswap-4 251 μs 174.5 μs 1.44
validation-decode-uniswap-5 1032 μs 710.8 μs 1.45
validation-decode-uniswap-6 196.1 μs 174.3 μs 1.13
nofib-clausify/formula1 3856 μs 3595 μs 1.07
marlowe-semantics/0000020002010200020101020201000100010001020101020201010000020102 454.7 μs 320.4 μs 1.42
marlowe-role-payout/06317060a8e488b1219c9dae427f9ce27918a9e09ee8ac424afa33ca923f7954 228.4 μs 180.9 μs 1.26
marlowe-role-payout/07658a6c898ad6d624c37df1e49e909c2e9349ba7f4c0a6be5f166fe239bfcae 228.4 μs 162.9 μs 1.40
marlowe-role-payout/0bdca1cb8fa7e38e09062557b82490714052e84e2054e913092cd84ac071b961 277.4 μs 198.7 μs 1.40
marlowe-role-payout/0c9d3634aeae7038f839a1262d1a8bc724dc77af9426459417a56ec73240f0e0 250 μs 178.2 μs 1.40
marlowe-role-payout/0d0f01050a0a0a0b0b050d0404090e0d0506000d0a041003040e0f100e0a0408 247.6 μs 202.1 μs 1.23
marlowe-role-payout/0dbb692d2bf22d25eeceac461cfebf616f54003077a8473abc0457f18e025960 280.8 μs 199.4 μs 1.41
marlowe-role-payout/0e00171d0f1e1f14070d0a00091f07101808021d081e1b120219081312081e15 242.9 μs 173.3 μs 1.40
marlowe-role-payout/1a20b465d48a585ffd622bd8dc26a498a3c12f930ab4feab3a5064cfb3bc536a 223.5 μs 187.4 μs 1.19
marlowe-role-payout/211e1b6c10260c4620074d2e372c260d38643a3d605f63772524034f0a4a7632 252.2 μs 180 μs 1.40
marlowe-role-payout/21a1426fb3fb3019d5dc93f210152e90b0a6e740ef509b1cdd423395f010e0ca 265.1 μs 188.9 μs 1.40
marlowe-role-payout/224ce46046fab9a17be4197622825f45cc0c59a6bd1604405148e43768c487ef 240.4 μs 171.1 μs 1.41
marlowe-role-payout/332c2b1c11383d1b373e1315201f1128010e0e1518332f273f141b23243f2a07 229.7 μs 164.7 μs 1.39
marlowe-role-payout/3565ee025317e065e8555eef288080276716366769aad89e03389f5ec4ce26d7 253 μs 181.2 μs 1.40
marlowe-role-payout/3569299fc986f5354d02e627a9eaa48ab46d5af52722307a0af72bae87e256dc 235.1 μs 169.3 μs 1.39
marlowe-role-payout/36866914aa07cf62ef36cf2cd64c7f240e3371e27bb9fff5464301678e809c40 234.9 μs 168.9 μs 1.39
marlowe-role-payout/371c10d2526fc0f09dbe9ed59e44dcd949270b27dc42035addd7ff9f7e0d05e7 286.7 μs 204.9 μs 1.40
marlowe-role-payout/3897ef714bba3e6821495b706c75f8d64264c3fdaa58a3826c808b5a768c303d 245.7 μs 177 μs 1.39

This comment was automatically generated by workflow using github-action-benchmark.

CC: @IntersectMBO/plutus-core

Please sign in to comment.