Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracing utilities; with cabal flags switching #39

Merged
merged 26 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
62d34e2
Add tracing utilities with CPP flag switching
TotallyNotChase Dec 28, 2021
f5656e7
Backpackify
TotallyNotChase Dec 28, 2021
2682224
Remove redundant option
TotallyNotChase Dec 28, 2021
08186f1
Finish up tracing utilities impl with backpack
TotallyNotChase Dec 28, 2021
e316aba
Properly format
TotallyNotChase Dec 28, 2021
857f99a
Improve formatting on the documentation
TotallyNotChase Dec 28, 2021
69ec9e6
Add documentation on using generic tracing
TotallyNotChase Dec 28, 2021
cb2400c
Fix incorrect package name in documentation
TotallyNotChase Dec 28, 2021
47e26cb
Add information on the possibility of importing all modules
TotallyNotChase Dec 29, 2021
7592706
Add more documentation on handling backpack
TotallyNotChase Dec 29, 2021
2facb11
Remove errant newline
TotallyNotChase Dec 29, 2021
bf9ae81
Merge branch 'master' into tracing
TotallyNotChase Dec 29, 2021
e8da1a9
Move test suite to a separate package
TotallyNotChase Dec 29, 2021
9b775ec
Add tests for `plutarch-trace`
TotallyNotChase Dec 29, 2021
36b6877
Remove haddocks from concrete implementations
TotallyNotChase Dec 29, 2021
6840493
Make `ptraceIfTrue`, and `ptraceIfFalse` Haskell level functions
TotallyNotChase Dec 29, 2021
1ee24c8
Fix incorrect mixin example
TotallyNotChase Dec 29, 2021
3e0417f
Prevent duplicate inlining of term
TotallyNotChase Dec 29, 2021
e14c729
Add more tests for tracing
TotallyNotChase Dec 29, 2021
0686409
Merge branch 'tracing' of github.com:Plutonomicon/plutarch into tracing
TotallyNotChase Dec 29, 2021
95a5bed
Fix formatting
TotallyNotChase Dec 29, 2021
7c61b91
Merge remote-tracking branch 'origin/master' into staging
L-as Dec 30, 2021
eb009fc
Fix name of examples project
L-as Dec 30, 2021
98b8e5f
Remove unnecessary `ptrace'`
L-as Dec 30, 2021
d3f93c4
Remove backpack; use cabal flags + CPP
TotallyNotChase Jan 4, 2022
f959a1d
Add `ptraceError` + some haddocks
TotallyNotChase Jan 4, 2022
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
58 changes: 58 additions & 0 deletions Plutarch/Trace.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{-# LANGUAGE CPP #-}

module Plutarch.Trace (ptrace, ptraceIfTrue, ptraceIfFalse, ptraceError) where

-- CPP support isn't great in fourmolu.
{- ORMOLU_DISABLE -}

#ifdef Development
import Plutarch (punsafeBuiltin)
#endif
#ifdef Development
import Plutarch.Bool (PBool, pif)
#else
import Plutarch.Bool (PBool)
#endif
import Plutarch.Prelude
import Plutarch.String (PString)

#ifdef Development
import qualified PlutusCore as PLC
#endif

#ifdef Development
ptrace' :: Term s (PString :--> a :--> a)
ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace
#endif

-- | Trace the given message before evaluating the argument.
ptrace :: Term s PString -> Term s a -> Term s a
#ifdef Development
ptrace s a = pforce $ ptrace' # s # pdelay a
#else
ptrace _ a = a
#endif

-- | Trace the given message and terminate evaluation with a 'perror'.
ptraceError :: Term s PString -> Term s a
#ifdef Development
ptraceError s = pforce $ ptrace' # s # pdelay perror
#else
ptraceError _ = perror
#endif

-- | Trace the given message if the argument evaluates to true.
ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool
#ifdef Development
ptraceIfTrue s a' = plet a' $ \a -> pif a (ptrace' # s # a) a
#else
ptraceIfTrue _ a = a
#endif

-- | Trace the given message if the argument evaluates to False.
ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool
#ifdef Development
ptraceIfFalse s a' = plet a' $ \a -> pif a a (ptrace' # s # a)
#else
ptraceIfFalse _ a = a
#endif
2 changes: 2 additions & 0 deletions examples/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import Plutarch.Evaluate (evaluateScript)
import Plutarch.Integer (PInteger)
import Plutarch.Prelude
import Plutarch.ScriptContext (PScriptPurpose (PMinting))
import Plutarch.Spec.Tracing (traceTests)
import Plutarch.String (PString, pfromText)
import Plutarch.Unit (PUnit (..))
import qualified Plutus.V1.Ledger.Scripts as Scripts
Expand Down Expand Up @@ -216,6 +217,7 @@ plutarchTests =
, testCase "PAsData equality" $ do
expect $ let dat = pdata @PInteger 42 in dat #== dat
expect $ pnot #$ pdata (phexByteStr "12") #== pdata (phexByteStr "ab")
, testCase "Tracing" $ traceTests
, testCase "λx y -> addInteger x y => addInteger" $
printTerm (plam $ \x y -> (x :: Term _ PInteger) + y) @?= "(program 1.0.0 addInteger)"
, testCase "λx y -> hoist (force mkCons) x y => force mkCons" $
Expand Down
76 changes: 76 additions & 0 deletions examples/Plutarch/Spec/Tracing.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
{-# LANGUAGE CPP #-}

module Plutarch.Spec.Tracing (traceTests) where

import Test.Tasty.HUnit

import Data.Text (Text)
import Plutarch
import Plutarch.Bool (PBool (PFalse, PTrue))
import Plutarch.Evaluate (evaluateScript)
import Plutarch.Trace (ptrace, ptraceIfFalse, ptraceIfTrue)
import Plutarch.Unit (PUnit (PUnit))

traces :: ClosedTerm a -> [Text] -> Assertion
traces x sl =
case evaluateScript $ compile x of
Left e -> assertFailure $ "Script evaluation failed: " <> show e
Right (_, traceLog, _) -> traceLog @?= sl

traceTests :: IO ()
traceTests = do

-- CPP support isn't great in fourmolu.
{- ORMOLU_DISABLE -}
ptrace "foo" (pcon PUnit) `traces`
#ifdef Development
["foo"]
#else
[]
#endif

ptrace "foo" (ptrace "bar" $ pcon PUnit) `traces`
#ifdef Development
["foo", "bar"]
#else
[]
#endif

ptraceIfTrue "foo" (pcon PTrue) `traces`
#ifdef Development
["foo"]
#else
[]
#endif

ptraceIfTrue "foo" (pcon PFalse) `traces` []

ptraceIfTrue "foo" (ptraceIfTrue "bar" $ pcon PTrue) `traces`
#ifdef Development
["bar", "foo"]
#else
[]
#endif

ptraceIfTrue "foo" (ptraceIfTrue "bar" $ pcon PFalse) `traces` []
ptraceIfFalse "foo" (ptraceIfTrue "bar" $ pcon PFalse) `traces`
#ifdef Development
["foo"]
#else
[]
#endif

ptrace "foo" (ptraceIfTrue "bar" (pcon PTrue)) `traces`
#ifdef Development
["foo", "bar"]
#else
[]
#endif

ptrace "foo" (ptraceIfTrue "bar" (pcon PFalse)) `traces`
#ifdef Development
["foo"]
#else
[]
#endif
{- ORMOLU_ENABLE -}
14 changes: 14 additions & 0 deletions plutarch.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ license: MIT

extra-source-files: README.md

flag development
description: Enable tracing functions within plutarch.
manual: True
default: False

common c
default-language: Haskell2010
default-extensions:
Expand Down Expand Up @@ -89,6 +94,7 @@ library
Plutarch.Maybe
Plutarch.Unit
Plutarch.Crypto
Plutarch.Trace
build-depends:
, base
, plutus-core
Expand All @@ -102,19 +108,27 @@ library
, mtl
, flat

if flag(development)
cpp-options: -DDevelopment

test-suite examples
import: c
type: exitcode-stdio-1.0
main-is: Main.hs
hs-source-dirs: examples
other-modules:
Plutarch.Spec.Tracing
build-depends:
, base
, bytestring
, text
, plutarch
, tasty
, tasty-hunit
, plutus-ledger-api
, plutus-core
, aeson
, plutus-tx

if flag(development)
cpp-options: -DDevelopment