From 62d34e2821900e9f959a38c1411509c01dc0ac83 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 14:08:34 +0300 Subject: [PATCH 01/23] Add tracing utilities with CPP flag switching --- Plutarch/Trace.hs | 70 +++++++++++++++++++++++++++++++++++++++++++++++ plutarch.cabal | 9 ++++++ 2 files changed, 79 insertions(+) create mode 100644 Plutarch/Trace.hs diff --git a/Plutarch/Trace.hs b/Plutarch/Trace.hs new file mode 100644 index 000000000..ccb62fdeb --- /dev/null +++ b/Plutarch/Trace.hs @@ -0,0 +1,70 @@ +{-# LANGUAGE CPP #-} + +module Plutarch.Trace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where + +import Plutarch ( + Term, + phoistAcyclic, + plam, +#ifdef Development + pforce, + pdelay, + punsafeBuiltin, + (#), +#endif + type (:-->) + ) +#ifdef Development +import Plutarch.Bool (PBool, pif) +#else +import Plutarch.Bool (PBool) +#endif +import Plutarch.String (PString) +#ifdef Development +import qualified PlutusCore as PLC +#endif + +-- | A strict version of 'ptrace'. +ptrace' :: Term s (PString :--> a :--> a) + +#ifdef Development +ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace +#else +ptrace' = phoistAcyclic $ plam f + where + f :: Term s PString -> Term s a -> Term s a + f _ a = a +#endif + +-- | Trace a message, then evaluate and return given 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 a message if given argument evaluates to true. +ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) + +#ifdef Development +ptraceIfTrue = phoistAcyclic $ plam $ \s a -> pif a (ptrace' # s # a) a +#else +ptraceIfTrue = phoistAcyclic $ plam f + where + f :: Term s PString -> Term s PBool -> Term s PBool + f _ a = a +#endif + +-- | Trace a message if given argument evaluates to false. +ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) + +#ifdef Development +ptraceIfFalse = phoistAcyclic $ plam $ \s a -> pif a a $ ptrace' # s # a +#else +ptraceIfFalse = phoistAcyclic $ plam f + where + f :: Term s PString -> Term s PBool -> Term s PBool + f _ a = a +#endif diff --git a/plutarch.cabal b/plutarch.cabal index fb077b939..7870aacd8 100644 --- a/plutarch.cabal +++ b/plutarch.cabal @@ -7,6 +7,11 @@ license: MIT extra-source-files: README.md +flag development + description: Enable tracing functions within plutarch. + manual: True + default: True + common c default-language: Haskell2010 default-extensions: @@ -88,6 +93,7 @@ library Plutarch.Maybe Plutarch.Unit Plutarch.Crypto + Plutarch.Trace build-depends: , base , plutus-core @@ -101,6 +107,9 @@ library , mtl , flat + if flag(development) + cpp-options: -DDevelopment + test-suite examples import: c type: exitcode-stdio-1.0 From f5656e7c292bf41c8d65df82b3852da348074e59 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 16:09:59 +0300 Subject: [PATCH 02/23] Backpackify --- Plutarch/Trace.hs | 70 ----------------- cabal.project | 2 + .../impl-enable/Plutarch/EnableTrace.hs | 22 ++++++ .../plutarch-tracing-enabled.cabal | 77 +++++++++++++++++++ plutarch-tracing/sig/Plutarch/Trace.hsig | 18 +++++ plutarch-tracing/sig/plutarch-tracing.cabal | 76 ++++++++++++++++++ plutarch.cabal | 9 --- 7 files changed, 195 insertions(+), 79 deletions(-) delete mode 100644 Plutarch/Trace.hs create mode 100644 plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs create mode 100644 plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal create mode 100644 plutarch-tracing/sig/Plutarch/Trace.hsig create mode 100644 plutarch-tracing/sig/plutarch-tracing.cabal diff --git a/Plutarch/Trace.hs b/Plutarch/Trace.hs deleted file mode 100644 index ccb62fdeb..000000000 --- a/Plutarch/Trace.hs +++ /dev/null @@ -1,70 +0,0 @@ -{-# LANGUAGE CPP #-} - -module Plutarch.Trace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where - -import Plutarch ( - Term, - phoistAcyclic, - plam, -#ifdef Development - pforce, - pdelay, - punsafeBuiltin, - (#), -#endif - type (:-->) - ) -#ifdef Development -import Plutarch.Bool (PBool, pif) -#else -import Plutarch.Bool (PBool) -#endif -import Plutarch.String (PString) -#ifdef Development -import qualified PlutusCore as PLC -#endif - --- | A strict version of 'ptrace'. -ptrace' :: Term s (PString :--> a :--> a) - -#ifdef Development -ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace -#else -ptrace' = phoistAcyclic $ plam f - where - f :: Term s PString -> Term s a -> Term s a - f _ a = a -#endif - --- | Trace a message, then evaluate and return given 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 a message if given argument evaluates to true. -ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) - -#ifdef Development -ptraceIfTrue = phoistAcyclic $ plam $ \s a -> pif a (ptrace' # s # a) a -#else -ptraceIfTrue = phoistAcyclic $ plam f - where - f :: Term s PString -> Term s PBool -> Term s PBool - f _ a = a -#endif - --- | Trace a message if given argument evaluates to false. -ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) - -#ifdef Development -ptraceIfFalse = phoistAcyclic $ plam $ \s a -> pif a a $ ptrace' # s # a -#else -ptraceIfFalse = phoistAcyclic $ plam f - where - f :: Term s PString -> Term s PBool -> Term s PBool - f _ a = a -#endif diff --git a/cabal.project b/cabal.project index 67f3700c2..c96c32632 100644 --- a/cabal.project +++ b/cabal.project @@ -1,3 +1,5 @@ index-state: 2021-11-18T00:00:00Z packages: ./. + plutarch-tracing/sig + plutarch-tracing/impl-enable \ No newline at end of file diff --git a/plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs b/plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs new file mode 100644 index 000000000..29987f5bc --- /dev/null +++ b/plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs @@ -0,0 +1,22 @@ +module Plutarch.EnableTrace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where + +import Plutarch +import Plutarch.Bool (PBool, pif) +import Plutarch.String (PString) +import qualified PlutusCore as PLC + +-- | A strict version of 'ptrace'. +ptrace' :: Term s (PString :--> a :--> a) +ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace + +-- | Trace a message, then evaluate and return given argument. +ptrace :: Term s PString -> Term s a -> Term s a +ptrace s a = pforce $ ptrace' # s # pdelay a + +-- | Trace a message if given argument evaluates to true. +ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) +ptraceIfTrue = phoistAcyclic $ plam $ \s a -> pif a (ptrace' # s # a) a + +-- | Trace a message if given argument evaluates to false. +ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) +ptraceIfFalse = phoistAcyclic $ plam $ \s a -> pif a a $ ptrace' # s # a diff --git a/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal b/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal new file mode 100644 index 000000000..aeb82ff4e --- /dev/null +++ b/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal @@ -0,0 +1,77 @@ +cabal-version: 2.4 +name: plutarch-tracing-enabled +version: 1.0.0 + +author: Chase +license: MIT + +common c + default-language: Haskell2010 + default-extensions: + DataKinds + DeriveAnyClass + DerivingStrategies + LambdaCase + TypeFamilies + OverloadedStrings + PartialTypeSignatures + -- poor man's GHC2021 + BangPatterns + BinaryLiterals + ConstrainedClassMethods + ConstraintKinds + DeriveDataTypeable + DeriveFoldable + DeriveFunctor + DeriveGeneric + DeriveLift + DeriveTraversable + DoAndIfThenElse + EmptyCase + EmptyDataDecls + EmptyDataDeriving + ExistentialQuantification + ExplicitForAll + FlexibleContexts + FlexibleInstances + ForeignFunctionInterface + GADTSyntax + GeneralisedNewtypeDeriving + HexFloatLiterals + ImplicitPrelude + ImportQualifiedPost + InstanceSigs + KindSignatures + MonomorphismRestriction + MultiParamTypeClasses + NamedFieldPuns + NamedWildCards + NumericUnderscores + PatternGuards + PolyKinds + PostfixOperators + RankNTypes + RelaxedPolyRec + ScopedTypeVariables + StandaloneDeriving + StandaloneKindSignatures + StarIsType + TraditionalRecordSyntax + TupleSections + TypeApplications + TypeOperators + TypeSynonymInstances + ghc-options: + -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures + -Wmissing-export-lists -Werror -Wincomplete-record-updates + -Wmissing-deriving-strategies -Wno-name-shadowing + +library + import: c + visibility: public + exposed-modules: + Plutarch.EnableTrace + build-depends: + , base + , plutarch + , plutus-core diff --git a/plutarch-tracing/sig/Plutarch/Trace.hsig b/plutarch-tracing/sig/Plutarch/Trace.hsig new file mode 100644 index 000000000..450d887c1 --- /dev/null +++ b/plutarch-tracing/sig/Plutarch/Trace.hsig @@ -0,0 +1,18 @@ +signature Plutarch.Trace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where + +import Prelude () +import Plutarch.Prelude (Term, (:-->)) +import Plutarch.String (PString) +import Plutarch.Bool (PBool) + +-- | A strict version of 'ptrace'. +ptrace' :: Term s (PString :--> a :--> a) + +-- | Trace a message, then evaluate and return given argument. +ptrace :: Term s PString -> Term s a -> Term s a + +-- | Trace a message if given argument evaluates to true. +ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) + +-- | Trace a message if given argument evaluates to false. +ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) diff --git a/plutarch-tracing/sig/plutarch-tracing.cabal b/plutarch-tracing/sig/plutarch-tracing.cabal new file mode 100644 index 000000000..4f13117ff --- /dev/null +++ b/plutarch-tracing/sig/plutarch-tracing.cabal @@ -0,0 +1,76 @@ +cabal-version: 2.4 +name: plutarch-tracing-sig +version: 1.0.0 + +author: Chase +license: MIT + +common c + default-language: Haskell2010 + default-extensions: + DataKinds + DeriveAnyClass + DerivingStrategies + LambdaCase + TypeFamilies + OverloadedStrings + PartialTypeSignatures + -- poor man's GHC2021 + BangPatterns + BinaryLiterals + ConstrainedClassMethods + ConstraintKinds + DeriveDataTypeable + DeriveFoldable + DeriveFunctor + DeriveGeneric + DeriveLift + DeriveTraversable + DoAndIfThenElse + EmptyCase + EmptyDataDecls + EmptyDataDeriving + ExistentialQuantification + ExplicitForAll + FlexibleContexts + FlexibleInstances + ForeignFunctionInterface + GADTSyntax + GeneralisedNewtypeDeriving + HexFloatLiterals + ImplicitPrelude + ImportQualifiedPost + InstanceSigs + KindSignatures + MonomorphismRestriction + MultiParamTypeClasses + NamedFieldPuns + NamedWildCards + NumericUnderscores + PatternGuards + PolyKinds + PostfixOperators + RankNTypes + RelaxedPolyRec + ScopedTypeVariables + StandaloneDeriving + StandaloneKindSignatures + StarIsType + TraditionalRecordSyntax + TupleSections + TypeApplications + TypeOperators + TypeSynonymInstances + ghc-options: + -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures + -Wmissing-export-lists -Werror -Wincomplete-record-updates + -Wmissing-deriving-strategies -Wno-name-shadowing + +library + import: c + visibility: public + signatures: + Plutarch.Trace + build-depends: + , base + , plutarch diff --git a/plutarch.cabal b/plutarch.cabal index 7870aacd8..fb077b939 100644 --- a/plutarch.cabal +++ b/plutarch.cabal @@ -7,11 +7,6 @@ license: MIT extra-source-files: README.md -flag development - description: Enable tracing functions within plutarch. - manual: True - default: True - common c default-language: Haskell2010 default-extensions: @@ -93,7 +88,6 @@ library Plutarch.Maybe Plutarch.Unit Plutarch.Crypto - Plutarch.Trace build-depends: , base , plutus-core @@ -107,9 +101,6 @@ library , mtl , flat - if flag(development) - cpp-options: -DDevelopment - test-suite examples import: c type: exitcode-stdio-1.0 From 268222462faefde7bf099cd392d4ee84991c721d Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 16:23:51 +0300 Subject: [PATCH 03/23] Remove redundant option --- plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal | 1 - plutarch-tracing/sig/plutarch-tracing.cabal | 1 - 2 files changed, 2 deletions(-) diff --git a/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal b/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal index aeb82ff4e..2181d5bee 100644 --- a/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal +++ b/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal @@ -68,7 +68,6 @@ common c library import: c - visibility: public exposed-modules: Plutarch.EnableTrace build-depends: diff --git a/plutarch-tracing/sig/plutarch-tracing.cabal b/plutarch-tracing/sig/plutarch-tracing.cabal index 4f13117ff..a57303f19 100644 --- a/plutarch-tracing/sig/plutarch-tracing.cabal +++ b/plutarch-tracing/sig/plutarch-tracing.cabal @@ -68,7 +68,6 @@ common c library import: c - visibility: public signatures: Plutarch.Trace build-depends: From 08186f1ba8ec41064f3ff5f92f85ab6500dd50c3 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 18:29:52 +0300 Subject: [PATCH 04/23] Finish up tracing utilities impl with backpack --- cabal.project | 3 +- plutarch-trace/README.md | 116 ++++++++++++++++++ plutarch-trace/impl/Plutarch/Trace/Disable.hs | 24 ++++ .../impl/Plutarch/Trace/Enable.hs | 2 +- .../plutarch-trace.cabal | 37 +++++- plutarch-trace/src/Plutarch/Trace.hs | 3 + .../src/Plutarch/TraceSig.hsig | 4 +- plutarch-tracing/sig/plutarch-tracing.cabal | 75 ----------- 8 files changed, 180 insertions(+), 84 deletions(-) create mode 100644 plutarch-trace/README.md create mode 100644 plutarch-trace/impl/Plutarch/Trace/Disable.hs rename plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs => plutarch-trace/impl/Plutarch/Trace/Enable.hs (90%) rename plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal => plutarch-trace/plutarch-trace.cabal (73%) create mode 100644 plutarch-trace/src/Plutarch/Trace.hs rename plutarch-tracing/sig/Plutarch/Trace.hsig => plutarch-trace/src/Plutarch/TraceSig.hsig (78%) delete mode 100644 plutarch-tracing/sig/plutarch-tracing.cabal diff --git a/cabal.project b/cabal.project index c96c32632..2958a851f 100644 --- a/cabal.project +++ b/cabal.project @@ -1,5 +1,4 @@ index-state: 2021-11-18T00:00:00Z packages: ./. - plutarch-tracing/sig - plutarch-tracing/impl-enable \ No newline at end of file + plutarch-trace diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md new file mode 100644 index 000000000..1f28b03de --- /dev/null +++ b/plutarch-trace/README.md @@ -0,0 +1,116 @@ +# plutarch-trace + +## Usage +You **need** `cabal-install >= 3.0` for this. Your package's cabal file **must** use a `cabal-version` of 3.0 or higher. + +* Add `plutarch-trace` to your `build-depends`. + +The next steps depend on whether or not you want tracing enabled. +### Enable Tracing +* Add `plutarch-trace:enable` to your `build-depends`. +* Add a mixin similar to the following- + + ``` + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + ``` + + This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with the real tracing functions. + + You can rename the `Plutarch.Trace` module if you want, to something else- + ``` + plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + ``` + This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. + +Example `.cabal` file- +```hs +cabal-version: 3.0 +name: foo +version: 1.0.0 + +executable foo-exe + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:enable + mixins: + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) + default-language: Haskell2010 +``` + +### Disable Tracing +* Add `plutarch-trace:disable` to your `build-depends`. +* Add a mixin similar to the following- + + ``` + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable), + ``` + + This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with dummy functions that don't actually trace. + + You can rename the `Plutarch.Trace` module if you want, to something else- + ``` + plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + ``` + This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. + +Example `.cabal` file- +```hs +cabal-version: 3.0 +name: foo +version: 1.0.0 + +executable foo-exe + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:disable + mixins: + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 +``` + +### Both! +Yuo want both huh? Well you can do that too! You can import `Plutarch.Trace` with two different setups and alias them to two different names. +* Add both `plutarch-trace:enable` and `plutarch-trace:disable` to your `build-depends`. +* Add both mixins- + + ``` + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + ``` + + Here, we set the *tracing enabled* module as `Plutarch.Trace`, and rename the *tracing disabled* module as `Plutarch.NoTrace`. Of course, you need different names for them. So be sure to rename them using `as`. + +Example `.cabal` file- +```hs +cabal-version: 3.0 +name: foo +version: 1.0.0 + +executable foo-exe + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:enable, + plutarch-trace:disable + mixins: + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 +``` + +## Contributing +### Learning Backpack +* [really-small-backpack-example](https://github.com/danidiaz/really-small-backpack-example) +* [backpack-str](https://github.com/haskell-backpack/backpack-str) + +### Common Issues +* Changed a few module names/structures and getting an error like "The library package ... does not require ..." or "The library package ... does not expose ..."? If you're sure you put the stuff in `signatures` and `exposed-modules` correctly - just do a `cabal clean` and rebuild. +* If GHCi reports that it cannot find interface files for the signatures when loading into `cabal repl` - it means whatever package you're loading into is using a generic signature and not instantiating it with any implementation. You cannot use the repl on these generic packages. You must instantiate it with a concrete impl in your `mixins`. diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs new file mode 100644 index 000000000..02f3da3f1 --- /dev/null +++ b/plutarch-trace/impl/Plutarch/Trace/Disable.hs @@ -0,0 +1,24 @@ +module Plutarch.Trace.Disable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where + +import Plutarch.Prelude +import Plutarch.Bool (PBool) +import Plutarch.String (PString) + +pf :: Term s (b :--> a :--> a) +pf = phoistAcyclic $ plam $ \_ y -> y + +-- | A strict version of 'ptrace'. +ptrace' :: Term s (PString :--> a :--> a) +ptrace' = pf + +-- | Trace a message, then evaluate and return given argument. +ptrace :: Term s PString -> Term s a -> Term s a +ptrace _ a = a + +-- | Trace a message if given argument evaluates to true. +ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) +ptraceIfTrue = pf + +-- | Trace a message if given argument evaluates to false. +ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) +ptraceIfFalse = pf diff --git a/plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs similarity index 90% rename from plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs rename to plutarch-trace/impl/Plutarch/Trace/Enable.hs index 29987f5bc..1396d5f14 100644 --- a/plutarch-tracing/impl-enable/Plutarch/EnableTrace.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -1,4 +1,4 @@ -module Plutarch.EnableTrace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where +module Plutarch.Trace.Enable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where import Plutarch import Plutarch.Bool (PBool, pif) diff --git a/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal b/plutarch-trace/plutarch-trace.cabal similarity index 73% rename from plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal rename to plutarch-trace/plutarch-trace.cabal index 2181d5bee..f53cbf057 100644 --- a/plutarch-tracing/impl-enable/plutarch-tracing-enabled.cabal +++ b/plutarch-trace/plutarch-trace.cabal @@ -1,10 +1,12 @@ -cabal-version: 2.4 -name: plutarch-tracing-enabled +cabal-version: 3.0 +name: plutarch-trace version: 1.0.0 -author: Chase +author: Las Safin , Chase license: MIT +extra-source-files: README.md + common c default-language: Haskell2010 default-extensions: @@ -68,9 +70,36 @@ common c library import: c + hs-source-dirs: src + signatures: + Plutarch.TraceSig + exposed-modules: + Plutarch.Trace + build-depends: + , base + , plutarch + +library enable + import: c + hs-source-dirs: + impl + visibility: + public exposed-modules: - Plutarch.EnableTrace + Plutarch.Trace.Enable build-depends: , base , plutarch , plutus-core + +library disable + import: c + hs-source-dirs: + impl + visibility: + public + exposed-modules: + Plutarch.Trace.Disable + build-depends: + , base + , plutarch diff --git a/plutarch-trace/src/Plutarch/Trace.hs b/plutarch-trace/src/Plutarch/Trace.hs new file mode 100644 index 000000000..c09719bf5 --- /dev/null +++ b/plutarch-trace/src/Plutarch/Trace.hs @@ -0,0 +1,3 @@ +module Plutarch.Trace (module Plutarch.TraceSig) where + +import Plutarch.TraceSig diff --git a/plutarch-tracing/sig/Plutarch/Trace.hsig b/plutarch-trace/src/Plutarch/TraceSig.hsig similarity index 78% rename from plutarch-tracing/sig/Plutarch/Trace.hsig rename to plutarch-trace/src/Plutarch/TraceSig.hsig index 450d887c1..b7c40a563 100644 --- a/plutarch-tracing/sig/Plutarch/Trace.hsig +++ b/plutarch-trace/src/Plutarch/TraceSig.hsig @@ -1,4 +1,4 @@ -signature Plutarch.Trace (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where +signature Plutarch.TraceSig (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where import Prelude () import Plutarch.Prelude (Term, (:-->)) @@ -15,4 +15,4 @@ ptrace :: Term s PString -> Term s a -> Term s a ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) -- | Trace a message if given argument evaluates to false. -ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) +ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) \ No newline at end of file diff --git a/plutarch-tracing/sig/plutarch-tracing.cabal b/plutarch-tracing/sig/plutarch-tracing.cabal deleted file mode 100644 index a57303f19..000000000 --- a/plutarch-tracing/sig/plutarch-tracing.cabal +++ /dev/null @@ -1,75 +0,0 @@ -cabal-version: 2.4 -name: plutarch-tracing-sig -version: 1.0.0 - -author: Chase -license: MIT - -common c - default-language: Haskell2010 - default-extensions: - DataKinds - DeriveAnyClass - DerivingStrategies - LambdaCase - TypeFamilies - OverloadedStrings - PartialTypeSignatures - -- poor man's GHC2021 - BangPatterns - BinaryLiterals - ConstrainedClassMethods - ConstraintKinds - DeriveDataTypeable - DeriveFoldable - DeriveFunctor - DeriveGeneric - DeriveLift - DeriveTraversable - DoAndIfThenElse - EmptyCase - EmptyDataDecls - EmptyDataDeriving - ExistentialQuantification - ExplicitForAll - FlexibleContexts - FlexibleInstances - ForeignFunctionInterface - GADTSyntax - GeneralisedNewtypeDeriving - HexFloatLiterals - ImplicitPrelude - ImportQualifiedPost - InstanceSigs - KindSignatures - MonomorphismRestriction - MultiParamTypeClasses - NamedFieldPuns - NamedWildCards - NumericUnderscores - PatternGuards - PolyKinds - PostfixOperators - RankNTypes - RelaxedPolyRec - ScopedTypeVariables - StandaloneDeriving - StandaloneKindSignatures - StarIsType - TraditionalRecordSyntax - TupleSections - TypeApplications - TypeOperators - TypeSynonymInstances - ghc-options: - -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures - -Wmissing-export-lists -Werror -Wincomplete-record-updates - -Wmissing-deriving-strategies -Wno-name-shadowing - -library - import: c - signatures: - Plutarch.Trace - build-depends: - , base - , plutarch From e316aba28fa86b096989ade5baafedcc8d3a2f86 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 18:39:33 +0300 Subject: [PATCH 05/23] Properly format --- plutarch-trace/impl/Plutarch/Trace/Disable.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs index 02f3da3f1..59bf44c17 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Disable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Disable.hs @@ -1,7 +1,7 @@ module Plutarch.Trace.Disable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where -import Plutarch.Prelude import Plutarch.Bool (PBool) +import Plutarch.Prelude import Plutarch.String (PString) pf :: Term s (b :--> a :--> a) From 857f99ac7f08df5df10d438ed91c7be9fbc2899d Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 18:51:39 +0300 Subject: [PATCH 06/23] Improve formatting on the documentation --- plutarch-trace/README.md | 74 ++++++++++++++++++++-------------------- 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 1f28b03de..95c5f38fa 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -10,68 +10,68 @@ The next steps depend on whether or not you want tracing enabled. * Add `plutarch-trace:enable` to your `build-depends`. * Add a mixin similar to the following- - ``` + ```cabal plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), ``` This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with the real tracing functions. You can rename the `Plutarch.Trace` module if you want, to something else- - ``` + ```cabal plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), ``` This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. Example `.cabal` file- -```hs +```cabal cabal-version: 3.0 name: foo version: 1.0.0 executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:enable - mixins: - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) - default-language: Haskell2010 + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:enable + mixins: + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) + default-language: Haskell2010 ``` ### Disable Tracing * Add `plutarch-trace:disable` to your `build-depends`. * Add a mixin similar to the following- - ``` + ```cabal plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable), ``` This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with dummy functions that don't actually trace. You can rename the `Plutarch.Trace` module if you want, to something else- - ``` + ```cabal plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), ``` This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. Example `.cabal` file- -```hs +```cabal cabal-version: 3.0 name: foo version: 1.0.0 executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:disable - mixins: - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:disable + mixins: + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 ``` ### Both! @@ -79,7 +79,7 @@ Yuo want both huh? Well you can do that too! You can import `Plutarch.Trace` wit * Add both `plutarch-trace:enable` and `plutarch-trace:disable` to your `build-depends`. * Add both mixins- - ``` + ```cabal plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) ``` @@ -87,23 +87,23 @@ Yuo want both huh? Well you can do that too! You can import `Plutarch.Trace` wit Here, we set the *tracing enabled* module as `Plutarch.Trace`, and rename the *tracing disabled* module as `Plutarch.NoTrace`. Of course, you need different names for them. So be sure to rename them using `as`. Example `.cabal` file- -```hs +```cabal cabal-version: 3.0 name: foo version: 1.0.0 executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:enable, - plutarch-trace:disable - mixins: - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 + main-is: + Main.hs + build-depends: + base, + plutarch-trace, + plutarch-trace:enable, + plutarch-trace:disable + mixins: + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 ``` ## Contributing From 69ec9e6ceb6082df4434cac284d9d8db495cc593 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 18:52:04 +0300 Subject: [PATCH 07/23] Add documentation on using generic tracing --- plutarch-trace/README.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 95c5f38fa..697a279c2 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -106,6 +106,39 @@ executable foo-exe default-language: Haskell2010 ``` +### Keep it generic +If you're a library author, you may want to leave the instantiation to the user of your library. In this case, you should simply add `plutarch-tracing` as a dependency and use `Plutarch.Trace` in your code. Ignore other deps, ignore mixins. + +Library `.cabal` file- +```cabal +cabal-version: 3.0 +name: foo +version: 1.0.0 + +lib + hs-source-dirs: + src + exposed-modules: + Foo + Bar + build-depends: + base, + plutarch-trace + default-language: Haskell2010 +``` + +You won't be able to load your project into the repl with this though. There is no interface file for the abstract `Plutarch.Trace` module. That will only exist after instantiation. See [Common Issues #2](#common-issues). + +Users of your must add your library in their `build-depends`, alongside `plutarch-trace:enable` or `plutarch-trace:disable` (or both), alongside the appropriate mixins- +```cabal + mixins: + foo (Foo Bar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) +``` + +> Note that it's *the `foo` library* that is instantiated with the concrete implementation. Every dependency that uses a generic signature needs to be instantiated like this. If there are more dependencies that are generic (e.g a `plutarch-tracing` dependency), all of them need to be instantiated individually (unless you want generic use out of them and want to leave instantiating out to the next user above). + +`foo` is the package name that uses `plutarch-tracing` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. + ## Contributing ### Learning Backpack * [really-small-backpack-example](https://github.com/danidiaz/really-small-backpack-example) From cb2400cbc488e8bcd8e87ae87ec1f27185d319a6 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 28 Dec 2021 18:53:18 +0300 Subject: [PATCH 08/23] Fix incorrect package name in documentation --- plutarch-trace/README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 697a279c2..2c308ffb9 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -107,7 +107,7 @@ executable foo-exe ``` ### Keep it generic -If you're a library author, you may want to leave the instantiation to the user of your library. In this case, you should simply add `plutarch-tracing` as a dependency and use `Plutarch.Trace` in your code. Ignore other deps, ignore mixins. +If you're a library author, you may want to leave the instantiation to the user of your library. In this case, you should simply add `plutarch-trace` as a dependency and use `Plutarch.Trace` in your code. Ignore other deps, ignore mixins. Library `.cabal` file- ```cabal @@ -135,9 +135,9 @@ Users of your must add your library in their `build-depends`, alongside `plutarc foo (Foo Bar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) ``` -> Note that it's *the `foo` library* that is instantiated with the concrete implementation. Every dependency that uses a generic signature needs to be instantiated like this. If there are more dependencies that are generic (e.g a `plutarch-tracing` dependency), all of them need to be instantiated individually (unless you want generic use out of them and want to leave instantiating out to the next user above). +> Note that it's *the `foo` library* that is instantiated with the concrete implementation. Every dependency that uses a generic signature needs to be instantiated like this. If there are more dependencies that are generic (e.g a `plutarch-trace` dependency), all of them need to be instantiated individually (unless you want generic use out of them and want to leave instantiating out to the next user above). -`foo` is the package name that uses `plutarch-tracing` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. +`foo` is the package name that uses `plutarch-trace` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. ## Contributing ### Learning Backpack From 47e26cb72da8af10e31831accbba12adadb35179 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 12:09:44 +0300 Subject: [PATCH 09/23] Add information on the possibility of importing all modules --- plutarch-trace/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 2c308ffb9..c3a980335 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -137,7 +137,7 @@ Users of your must add your library in their `build-depends`, alongside `plutarc > Note that it's *the `foo` library* that is instantiated with the concrete implementation. Every dependency that uses a generic signature needs to be instantiated like this. If there are more dependencies that are generic (e.g a `plutarch-trace` dependency), all of them need to be instantiated individually (unless you want generic use out of them and want to leave instantiating out to the next user above). -`foo` is the package name that uses `plutarch-trace` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. +`foo` is the package name that uses `plutarch-trace` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. If you don't want to rename any of the modules, you can actually just leave out the module list in parens and just write `foo requires ...` to make all modules visible with their original name. ## Contributing ### Learning Backpack From 7592706c219c7115a9f25b2e89f58acda15f4b49 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 12:10:57 +0300 Subject: [PATCH 10/23] Add more documentation on handling backpack --- plutarch-trace/README.md | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index c3a980335..6e7e2bd69 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -139,9 +139,55 @@ Users of your must add your library in their `build-depends`, alongside `plutarc `foo` is the package name that uses `plutarch-trace` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. If you don't want to rename any of the modules, you can actually just leave out the module list in parens and just write `foo requires ...` to make all modules visible with their original name. +### Using flags to enable/disable tracing +One of the great things about backpack is that it interacts very well with cabal flags. You can keep your entire dependency hierarchy completely generic and only instantiate the roots of the depenedency hierarchy in your end-user executable/benchmark/test suite. While doing so, you might still want easy control over switching the tracing behavior. + +In this case, you can use a cabal flag to add the corresponding implementation dependency and mixins. Because backpack mixins are defined in the `.cabal` file, you can easily use `if` statements with a flag to control this- +```hs +cabal-version: 3.0 +name: foo +version: 1.0.0 + +flag development + description: Enable development mode (turns on tracing). + manual: True + default: True + +executable exm-exe + main-is: + Main.hs + build-depends: + base, + plutarch, + plutarch-trace, + if flag(development) + build-depends: plutarch-trace:enable + else + build-depends: plutarch-trace:disable + if flag(development) + mixins: plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) + else + mixins: plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 +``` + +As you'd expect, you can use `cabal configure -f +development` and `cabal configure -f -development` to easily switch the tracing behaviors! + +### Hiding Backpack implementation details from your user +Here's *another* neat thing about backpack - if you want, you can totally hide the backpack implementation details and let your users use good ol' cabal flags instead of `mixins`. This is actually one of the usecases noted in the [cabal user guide](https://cabal.readthedocs.io/en/3.6/cabal-package.html#backpack) itself. +> You may also find it useful to use library:reexported-modules to reexport instantiated libraries to Backpack-unware users (e.g., Backpack can be used entirely as an implementation detail.) + +In essence, you would have a package that [instantiates both](#both) tracing behaviors as 2 modules and just re-exports them depending on cabal flags! + + ## Contributing ### Learning Backpack * [really-small-backpack-example](https://github.com/danidiaz/really-small-backpack-example) + + lesson2 is very useful to immediately get up and running! +* [cabal user guide for backpack](https://cabal.readthedocs.io/en/3.6/cabal-package.html#backpack) + + I think this was recently added. * [backpack-str](https://github.com/haskell-backpack/backpack-str) ### Common Issues From 2facb1119cf82b097bc080a1cdf7f6958a605c12 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 12:31:27 +0300 Subject: [PATCH 11/23] Remove errant newline --- plutarch-trace/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 6e7e2bd69..7a2b5e014 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -179,7 +179,6 @@ Here's *another* neat thing about backpack - if you want, you can totally hide t In essence, you would have a package that [instantiates both](#both) tracing behaviors as 2 modules and just re-exports them depending on cabal flags! - ## Contributing ### Learning Backpack * [really-small-backpack-example](https://github.com/danidiaz/really-small-backpack-example) From e8da1a976d521e1b897a4aa28210b4052523c0b4 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 13:56:45 +0300 Subject: [PATCH 12/23] Move test suite to a separate package --- cabal.project | 1 + examples/examples.cabal | 87 +++++++++++++++++++++++++++++++++++++++++ plutarch.cabal | 82 +++++++++++++++----------------------- 3 files changed, 119 insertions(+), 51 deletions(-) create mode 100644 examples/examples.cabal diff --git a/cabal.project b/cabal.project index 2958a851f..9532d1286 100644 --- a/cabal.project +++ b/cabal.project @@ -2,3 +2,4 @@ index-state: 2021-11-18T00:00:00Z packages: ./. plutarch-trace + examples \ No newline at end of file diff --git a/examples/examples.cabal b/examples/examples.cabal new file mode 100644 index 000000000..20a4b7f75 --- /dev/null +++ b/examples/examples.cabal @@ -0,0 +1,87 @@ +cabal-version: 3.0 +name: plutarch-test +version: 1.0.0 + +author: Las Safin +license: MIT + +test-suite examples + type: exitcode-stdio-1.0 + main-is: Main.hs + other-modules: + build-depends: + , base + , bytestring + , plutarch + , plutarch-trace + , plutarch-trace:enable + , plutarch-trace:disable + , tasty + , tasty-hunit + , plutus-ledger-api + , plutus-core + , aeson + , plutus-tx + mixins: + plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) + default-language: Haskell2010 + default-extensions: + DataKinds + DeriveAnyClass + DerivingStrategies + LambdaCase + TypeFamilies + OverloadedStrings + PartialTypeSignatures + ViewPatterns + -- poor man's GHC2021 + BangPatterns + BinaryLiterals + ConstrainedClassMethods + ConstraintKinds + DeriveDataTypeable + DeriveFoldable + DeriveFunctor + DeriveGeneric + DeriveLift + DeriveTraversable + DoAndIfThenElse + EmptyCase + EmptyDataDecls + EmptyDataDeriving + ExistentialQuantification + ExplicitForAll + FlexibleContexts + FlexibleInstances + ForeignFunctionInterface + GADTSyntax + GeneralisedNewtypeDeriving + HexFloatLiterals + ImplicitPrelude + ImportQualifiedPost + InstanceSigs + KindSignatures + MonomorphismRestriction + MultiParamTypeClasses + NamedFieldPuns + NamedWildCards + NumericUnderscores + PatternGuards + PolyKinds + PostfixOperators + RankNTypes + RelaxedPolyRec + ScopedTypeVariables + StandaloneDeriving + StandaloneKindSignatures + StarIsType + TraditionalRecordSyntax + TupleSections + TypeApplications + TypeOperators + TypeSynonymInstances + ghc-options: + -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures + -Wmissing-export-lists -Werror -Wincomplete-record-updates + -Wmissing-deriving-strategies -Wno-name-shadowing diff --git a/plutarch.cabal b/plutarch.cabal index 20a3a16e1..0015c9c07 100644 --- a/plutarch.cabal +++ b/plutarch.cabal @@ -7,7 +7,37 @@ license: MIT extra-source-files: README.md -common c +library + exposed-modules: + Plutarch.Evaluate + Plutarch.Internal + Plutarch + Plutarch.Prelude + Plutarch.Pair + Plutarch.Rational + Plutarch.Either + Plutarch.Integer + Plutarch.Bool + Plutarch.String + Plutarch.ByteString + Plutarch.Builtin + Plutarch.DataRepr + Plutarch.ScriptContext + Plutarch.Maybe + Plutarch.Unit + Plutarch.Crypto + build-depends: + , base + , plutus-core + , plutus-ledger-api + , text + , hashable + , bytestring + , containers + , cryptonite + , plutus-tx + , mtl + , flat default-language: Haskell2010 default-extensions: DataKinds @@ -68,53 +98,3 @@ common c -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures -Wmissing-export-lists -Werror -Wincomplete-record-updates -Wmissing-deriving-strategies -Wno-name-shadowing - -library - import: c - exposed-modules: - Plutarch.Evaluate - Plutarch.Internal - Plutarch - Plutarch.Prelude - Plutarch.Pair - Plutarch.Rational - Plutarch.Either - Plutarch.Integer - Plutarch.Bool - Plutarch.String - Plutarch.ByteString - Plutarch.Builtin - Plutarch.DataRepr - Plutarch.ScriptContext - Plutarch.Maybe - Plutarch.Unit - Plutarch.Crypto - build-depends: - , base - , plutus-core - , plutus-ledger-api - , text - , hashable - , bytestring - , containers - , cryptonite - , plutus-tx - , mtl - , flat - -test-suite examples - import: c - type: exitcode-stdio-1.0 - main-is: Main.hs - hs-source-dirs: examples - other-modules: - build-depends: - , base - , bytestring - , plutarch - , tasty - , tasty-hunit - , plutus-ledger-api - , plutus-core - , aeson - , plutus-tx From 9b775ec1fc1abaa8e7c6e4289a7cf9704134a94a Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 14:19:18 +0300 Subject: [PATCH 13/23] Add tests for `plutarch-trace` --- examples/Main.hs | 20 ++++++++++++++++++++ examples/examples.cabal | 1 + 2 files changed, 21 insertions(+) diff --git a/examples/Main.hs b/examples/Main.hs index 074b227b4..29643c42f 100644 --- a/examples/Main.hs +++ b/examples/Main.hs @@ -9,6 +9,7 @@ import Control.Exception (SomeException, try) import qualified Data.Aeson as Aeson import qualified Data.ByteString as BS import Data.Maybe (fromJust) +import Data.Text (Text) import Plutarch (ClosedTerm, POpaque, compile, popaque, printScript, printTerm, punsafeBuiltin, punsafeCoerce, punsafeConstant) import Plutarch.Bool (PBool (PFalse, PTrue), pif, pnot, (#&&), (#<), (#<=), (#==), (#||)) import Plutarch.Builtin (PBuiltinList, PBuiltinPair, PData, pdata, pdataLiteral) @@ -16,9 +17,11 @@ import Plutarch.ByteString (pbyteStr, pconsBS, phexByteStr, pindexBS, plengthBS, import Plutarch.Either (PEither (PLeft, PRight)) import Plutarch.Evaluate (evaluateScript) import Plutarch.Integer (PInteger) +import qualified Plutarch.NoTrace as PNoTrace import Plutarch.Prelude import Plutarch.ScriptContext (PScriptPurpose (PMinting)) import Plutarch.String (PString, pfromText) +import qualified Plutarch.Trace as PTrace import Plutarch.Unit (PUnit (..)) import qualified Plutus.V1.Ledger.Scripts as Scripts import Plutus.V1.Ledger.Value (CurrencySymbol (CurrencySymbol)) @@ -91,6 +94,12 @@ throws x = Right _ -> assertFailure "Supposed to throw" Left _ -> pure () +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 + -- FIXME: Make the below impossible using run-time checks. -- loop :: Term (PInteger :--> PInteger) -- loop = plam $ \x -> loop # x @@ -216,6 +225,17 @@ plutarchTests = , testCase "PAsData equality" $ do expect $ let dat = pdata @PInteger 42 in dat #== dat expect $ pnot #$ pdata (phexByteStr "12") #== pdata (phexByteStr "ab") + , testCase "Tracing" $ do + -- Real tracing functions + PTrace.ptrace "foo" (pcon PUnit) `traces` ["foo"] + PTrace.ptrace "foo" (PTrace.ptrace "bar" $ pcon PUnit) `traces` ["foo", "bar"] + (PTrace.ptraceIfTrue # "foo" # pcon PTrue) `traces` ["foo"] + (PTrace.ptraceIfTrue # "foo" # pcon PFalse) `traces` [] + -- Dummy tracing functions + PNoTrace.ptrace "foo" (pcon PUnit) `traces` [] + PNoTrace.ptrace "foo" (PNoTrace.ptrace "bar" $ pcon PUnit) `traces` [] + (PNoTrace.ptraceIfTrue # "foo" # pcon PTrue) `traces` [] + (PNoTrace.ptraceIfTrue # "foo" # pcon PFalse) `traces` [] ] uplcTests :: TestTree diff --git a/examples/examples.cabal b/examples/examples.cabal index 20a4b7f75..8d3e83e78 100644 --- a/examples/examples.cabal +++ b/examples/examples.cabal @@ -12,6 +12,7 @@ test-suite examples build-depends: , base , bytestring + , text , plutarch , plutarch-trace , plutarch-trace:enable From 36b6877ca7c1744446a245525012475346705c68 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 14:46:28 +0300 Subject: [PATCH 14/23] Remove haddocks from concrete implementations --- plutarch-trace/impl/Plutarch/Trace/Disable.hs | 4 ---- plutarch-trace/impl/Plutarch/Trace/Enable.hs | 7 ++----- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs index 59bf44c17..70a73c52d 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Disable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Disable.hs @@ -7,18 +7,14 @@ import Plutarch.String (PString) pf :: Term s (b :--> a :--> a) pf = phoistAcyclic $ plam $ \_ y -> y --- | A strict version of 'ptrace'. ptrace' :: Term s (PString :--> a :--> a) ptrace' = pf --- | Trace a message, then evaluate and return given argument. ptrace :: Term s PString -> Term s a -> Term s a ptrace _ a = a --- | Trace a message if given argument evaluates to true. ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) ptraceIfTrue = pf --- | Trace a message if given argument evaluates to false. ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) ptraceIfFalse = pf diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs index 1396d5f14..af574d8dd 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -1,22 +1,19 @@ module Plutarch.Trace.Enable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where -import Plutarch +import Plutarch (punsafeBuiltin) import Plutarch.Bool (PBool, pif) +import Plutarch.Prelude import Plutarch.String (PString) import qualified PlutusCore as PLC --- | A strict version of 'ptrace'. ptrace' :: Term s (PString :--> a :--> a) ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace --- | Trace a message, then evaluate and return given argument. ptrace :: Term s PString -> Term s a -> Term s a ptrace s a = pforce $ ptrace' # s # pdelay a --- | Trace a message if given argument evaluates to true. ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) ptraceIfTrue = phoistAcyclic $ plam $ \s a -> pif a (ptrace' # s # a) a --- | Trace a message if given argument evaluates to false. ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) ptraceIfFalse = phoistAcyclic $ plam $ \s a -> pif a a $ ptrace' # s # a From 6840493d3d7ef51b75ea3e15ea933048731f283c Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 14:49:44 +0300 Subject: [PATCH 15/23] Make `ptraceIfTrue`, and `ptraceIfFalse` Haskell level functions --- examples/Main.hs | 8 ++++---- plutarch-trace/impl/Plutarch/Trace/Disable.hs | 13 +++++-------- plutarch-trace/impl/Plutarch/Trace/Enable.hs | 8 ++++---- plutarch-trace/src/Plutarch/TraceSig.hsig | 4 ++-- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/examples/Main.hs b/examples/Main.hs index 29643c42f..fc3ba825d 100644 --- a/examples/Main.hs +++ b/examples/Main.hs @@ -229,13 +229,13 @@ plutarchTests = -- Real tracing functions PTrace.ptrace "foo" (pcon PUnit) `traces` ["foo"] PTrace.ptrace "foo" (PTrace.ptrace "bar" $ pcon PUnit) `traces` ["foo", "bar"] - (PTrace.ptraceIfTrue # "foo" # pcon PTrue) `traces` ["foo"] - (PTrace.ptraceIfTrue # "foo" # pcon PFalse) `traces` [] + PTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` ["foo"] + PTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] -- Dummy tracing functions PNoTrace.ptrace "foo" (pcon PUnit) `traces` [] PNoTrace.ptrace "foo" (PNoTrace.ptrace "bar" $ pcon PUnit) `traces` [] - (PNoTrace.ptraceIfTrue # "foo" # pcon PTrue) `traces` [] - (PNoTrace.ptraceIfTrue # "foo" # pcon PFalse) `traces` [] + PNoTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` [] + PNoTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] ] uplcTests :: TestTree diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs index 70a73c52d..bef07ae80 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Disable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Disable.hs @@ -4,17 +4,14 @@ import Plutarch.Bool (PBool) import Plutarch.Prelude import Plutarch.String (PString) -pf :: Term s (b :--> a :--> a) -pf = phoistAcyclic $ plam $ \_ y -> y - ptrace' :: Term s (PString :--> a :--> a) -ptrace' = pf +ptrace' = phoistAcyclic $ plam $ \_ y -> y ptrace :: Term s PString -> Term s a -> Term s a ptrace _ a = a -ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) -ptraceIfTrue = pf +ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool +ptraceIfTrue _ a = a -ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) -ptraceIfFalse = pf +ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool +ptraceIfFalse _ a = a diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs index af574d8dd..f3614f548 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -12,8 +12,8 @@ ptrace' = phoistAcyclic $ pforce $ punsafeBuiltin PLC.Trace ptrace :: Term s PString -> Term s a -> Term s a ptrace s a = pforce $ ptrace' # s # pdelay a -ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) -ptraceIfTrue = phoistAcyclic $ plam $ \s a -> pif a (ptrace' # s # a) a +ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool +ptraceIfTrue s a = pif a (ptrace' # s # a) a -ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) -ptraceIfFalse = phoistAcyclic $ plam $ \s a -> pif a a $ ptrace' # s # a +ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool +ptraceIfFalse s a = pif a a $ ptrace' # s # a diff --git a/plutarch-trace/src/Plutarch/TraceSig.hsig b/plutarch-trace/src/Plutarch/TraceSig.hsig index b7c40a563..d8bf45161 100644 --- a/plutarch-trace/src/Plutarch/TraceSig.hsig +++ b/plutarch-trace/src/Plutarch/TraceSig.hsig @@ -12,7 +12,7 @@ ptrace' :: Term s (PString :--> a :--> a) ptrace :: Term s PString -> Term s a -> Term s a -- | Trace a message if given argument evaluates to true. -ptraceIfTrue :: Term s (PString :--> PBool :--> PBool) +ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool -- | Trace a message if given argument evaluates to false. -ptraceIfFalse :: Term s (PString :--> PBool :--> PBool) \ No newline at end of file +ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool \ No newline at end of file From 1ee24c880ea4e592e50fa55bdc2722830d251cdb Mon Sep 17 00:00:00 2001 From: Chase <44284917+TotallyNotChase@users.noreply.github.com> Date: Wed, 29 Dec 2021 15:28:29 +0300 Subject: [PATCH 16/23] Fix incorrect mixin example --- plutarch-trace/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md index 7a2b5e014..e859ccb14 100644 --- a/plutarch-trace/README.md +++ b/plutarch-trace/README.md @@ -52,7 +52,7 @@ executable foo-exe You can rename the `Plutarch.Trace` module if you want, to something else- ```cabal - plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), + plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Disable), ``` This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. From 3e0417f26457c2b0c677201fcdbfe9e0b57f4ce2 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 15:41:07 +0300 Subject: [PATCH 17/23] Prevent duplicate inlining of term --- plutarch-trace/impl/Plutarch/Trace/Enable.hs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs index f3614f548..5be093ac0 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -13,7 +13,6 @@ ptrace :: Term s PString -> Term s a -> Term s a ptrace s a = pforce $ ptrace' # s # pdelay a ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool -ptraceIfTrue s a = pif a (ptrace' # s # a) a - +ptraceIfTrue s a' = plet a' $ \a -> pif a (ptrace' # s # a) a ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool -ptraceIfFalse s a = pif a a $ ptrace' # s # a +ptraceIfFalse s a' = plet a' $ \a -> pif a a (ptrace' # s # a) From e14c729b2473eac1ad899648311a400730bde127 Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 15:41:14 +0300 Subject: [PATCH 18/23] Add more tests for tracing --- examples/Main.hs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/Main.hs b/examples/Main.hs index fc3ba825d..937f9997d 100644 --- a/examples/Main.hs +++ b/examples/Main.hs @@ -231,11 +231,21 @@ plutarchTests = PTrace.ptrace "foo" (PTrace.ptrace "bar" $ pcon PUnit) `traces` ["foo", "bar"] PTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` ["foo"] PTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] + PTrace.ptraceIfTrue "foo" (PTrace.ptraceIfTrue "bar" $ pcon PTrue) `traces` ["bar", "foo"] + PTrace.ptraceIfTrue "foo" (PTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] + PTrace.ptraceIfFalse "foo" (PTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` ["foo"] + PTrace.ptrace "foo" (PTrace.ptraceIfTrue "bar" (pcon PTrue)) `traces` ["foo", "bar"] + PTrace.ptrace "foo" (PTrace.ptraceIfTrue "bar" (pcon PFalse)) `traces` ["foo"] -- Dummy tracing functions PNoTrace.ptrace "foo" (pcon PUnit) `traces` [] PNoTrace.ptrace "foo" (PNoTrace.ptrace "bar" $ pcon PUnit) `traces` [] PNoTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` [] PNoTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] + PNoTrace.ptraceIfTrue "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PTrue) `traces` [] + PNoTrace.ptraceIfTrue "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] + PNoTrace.ptraceIfFalse "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] + PNoTrace.ptrace "foo" (PNoTrace.ptraceIfTrue "bar" (pcon PTrue)) `traces` [] + PNoTrace.ptrace "foo" (PNoTrace.ptraceIfTrue "bar" (pcon PFalse)) `traces` [] ] uplcTests :: TestTree From 95a5bed5d8a2d28b38bc8081e0869386dd0ee69d Mon Sep 17 00:00:00 2001 From: Chase Date: Wed, 29 Dec 2021 15:43:06 +0300 Subject: [PATCH 19/23] Fix formatting --- plutarch-trace/impl/Plutarch/Trace/Enable.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs index 5be093ac0..aa1d2e3ad 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -14,5 +14,6 @@ ptrace s a = pforce $ ptrace' # s # pdelay a ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool ptraceIfTrue s a' = plet a' $ \a -> pif a (ptrace' # s # a) a + ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool ptraceIfFalse s a' = plet a' $ \a -> pif a a (ptrace' # s # a) From eb009fc3db1b65bb17a3fa0f5d351acc9e439c87 Mon Sep 17 00:00:00 2001 From: Las Safin Date: Thu, 30 Dec 2021 10:36:15 +0000 Subject: [PATCH 20/23] Fix name of examples project Otherwise haskell.nix won't detect it --- cabal.project | 2 +- examples/examples.cabal | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cabal.project b/cabal.project index 9532d1286..65b78cd31 100644 --- a/cabal.project +++ b/cabal.project @@ -2,4 +2,4 @@ index-state: 2021-11-18T00:00:00Z packages: ./. plutarch-trace - examples \ No newline at end of file + examples diff --git a/examples/examples.cabal b/examples/examples.cabal index 8d3e83e78..298582291 100644 --- a/examples/examples.cabal +++ b/examples/examples.cabal @@ -1,5 +1,5 @@ cabal-version: 3.0 -name: plutarch-test +name: examples version: 1.0.0 author: Las Safin From 98b8e5f47f5bea7ac9af8513ee76734461a51742 Mon Sep 17 00:00:00 2001 From: Las Safin Date: Thu, 30 Dec 2021 10:45:29 +0000 Subject: [PATCH 21/23] Remove unnecessary `ptrace'` It is more efficient, albeit if you care about efficiency you'd just disable traces entirely, and `ptrace'` can't be elided completely, unlike `ptrace`. --- plutarch-trace/impl/Plutarch/Trace/Disable.hs | 5 +---- plutarch-trace/impl/Plutarch/Trace/Enable.hs | 2 +- plutarch-trace/src/Plutarch/TraceSig.hsig | 9 +++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs index bef07ae80..28d746afb 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Disable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Disable.hs @@ -1,12 +1,9 @@ -module Plutarch.Trace.Disable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where +module Plutarch.Trace.Disable (ptrace, ptraceIfTrue, ptraceIfFalse) where import Plutarch.Bool (PBool) import Plutarch.Prelude import Plutarch.String (PString) -ptrace' :: Term s (PString :--> a :--> a) -ptrace' = phoistAcyclic $ plam $ \_ y -> y - ptrace :: Term s PString -> Term s a -> Term s a ptrace _ a = a diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/plutarch-trace/impl/Plutarch/Trace/Enable.hs index aa1d2e3ad..191e464b8 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/plutarch-trace/impl/Plutarch/Trace/Enable.hs @@ -1,4 +1,4 @@ -module Plutarch.Trace.Enable (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where +module Plutarch.Trace.Enable (ptrace, ptraceIfTrue, ptraceIfFalse) where import Plutarch (punsafeBuiltin) import Plutarch.Bool (PBool, pif) diff --git a/plutarch-trace/src/Plutarch/TraceSig.hsig b/plutarch-trace/src/Plutarch/TraceSig.hsig index d8bf45161..e5fbec980 100644 --- a/plutarch-trace/src/Plutarch/TraceSig.hsig +++ b/plutarch-trace/src/Plutarch/TraceSig.hsig @@ -1,13 +1,10 @@ -signature Plutarch.TraceSig (ptrace', ptrace, ptraceIfTrue, ptraceIfFalse) where +signature Plutarch.TraceSig (ptrace, ptraceIfTrue, ptraceIfFalse) where import Prelude () -import Plutarch.Prelude (Term, (:-->)) +import Plutarch (Term) import Plutarch.String (PString) import Plutarch.Bool (PBool) --- | A strict version of 'ptrace'. -ptrace' :: Term s (PString :--> a :--> a) - -- | Trace a message, then evaluate and return given argument. ptrace :: Term s PString -> Term s a -> Term s a @@ -15,4 +12,4 @@ ptrace :: Term s PString -> Term s a -> Term s a ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool -- | Trace a message if given argument evaluates to false. -ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool \ No newline at end of file +ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool From d3f93c417e3ee479dcda8ab90a67f9a90afbfac8 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 4 Jan 2022 11:54:12 +0530 Subject: [PATCH 22/23] Remove backpack; use cabal flags + CPP --- .../Trace/Enable.hs => Plutarch/Trace.hs | 31 ++- cabal.project | 2 - examples/Main.hs | 32 +-- examples/Plutarch/Spec/Tracing.hs | 76 +++++++ examples/examples.cabal | 88 -------- plutarch-trace/README.md | 194 ------------------ plutarch-trace/impl/Plutarch/Trace/Disable.hs | 14 -- plutarch-trace/plutarch-trace.cabal | 105 ---------- plutarch-trace/src/Plutarch/Trace.hs | 3 - plutarch-trace/src/Plutarch/TraceSig.hsig | 15 -- plutarch.cabal | 96 ++++++--- 11 files changed, 173 insertions(+), 483 deletions(-) rename plutarch-trace/impl/Plutarch/Trace/Enable.hs => Plutarch/Trace.hs (57%) create mode 100644 examples/Plutarch/Spec/Tracing.hs delete mode 100644 examples/examples.cabal delete mode 100644 plutarch-trace/README.md delete mode 100644 plutarch-trace/impl/Plutarch/Trace/Disable.hs delete mode 100644 plutarch-trace/plutarch-trace.cabal delete mode 100644 plutarch-trace/src/Plutarch/Trace.hs delete mode 100644 plutarch-trace/src/Plutarch/TraceSig.hsig diff --git a/plutarch-trace/impl/Plutarch/Trace/Enable.hs b/Plutarch/Trace.hs similarity index 57% rename from plutarch-trace/impl/Plutarch/Trace/Enable.hs rename to Plutarch/Trace.hs index 191e464b8..c0bc5a06a 100644 --- a/plutarch-trace/impl/Plutarch/Trace/Enable.hs +++ b/Plutarch/Trace.hs @@ -1,19 +1,48 @@ -module Plutarch.Trace.Enable (ptrace, ptraceIfTrue, ptraceIfFalse) where +{-# LANGUAGE CPP #-} +module Plutarch.Trace (ptrace, ptraceIfTrue, ptraceIfFalse) 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 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 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 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 diff --git a/cabal.project b/cabal.project index 65b78cd31..67f3700c2 100644 --- a/cabal.project +++ b/cabal.project @@ -1,5 +1,3 @@ index-state: 2021-11-18T00:00:00Z packages: ./. - plutarch-trace - examples diff --git a/examples/Main.hs b/examples/Main.hs index 36e56f3e2..fdb2fca41 100644 --- a/examples/Main.hs +++ b/examples/Main.hs @@ -9,7 +9,6 @@ import Control.Exception (SomeException, try) import qualified Data.Aeson as Aeson import qualified Data.ByteString as BS import Data.Maybe (fromJust) -import Data.Text (Text) import Plutarch (ClosedTerm, POpaque, compile, popaque, printScript, printTerm, punsafeBuiltin, punsafeCoerce, punsafeConstant) import Plutarch.Bool (PBool (PFalse, PTrue), pif, pnot, (#&&), (#<), (#<=), (#==), (#||)) import Plutarch.Builtin (PBuiltinList, PBuiltinPair, PData, pdata, pdataLiteral) @@ -17,11 +16,10 @@ import Plutarch.ByteString (pbyteStr, pconsBS, phexByteStr, pindexBS, plengthBS, import Plutarch.Either (PEither (PLeft, PRight)) import Plutarch.Evaluate (evaluateScript) import Plutarch.Integer (PInteger) -import qualified Plutarch.NoTrace as PNoTrace import Plutarch.Prelude import Plutarch.ScriptContext (PScriptPurpose (PMinting)) +import Plutarch.Spec.Tracing (traceTests) import Plutarch.String (PString, pfromText) -import qualified Plutarch.Trace as PTrace import Plutarch.Unit (PUnit (..)) import qualified Plutus.V1.Ledger.Scripts as Scripts import Plutus.V1.Ledger.Value (CurrencySymbol (CurrencySymbol)) @@ -94,12 +92,6 @@ throws x = Right _ -> assertFailure "Supposed to throw" Left _ -> pure () -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 - -- FIXME: Make the below impossible using run-time checks. -- loop :: Term (PInteger :--> PInteger) -- loop = plam $ \x -> loop # x @@ -225,27 +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" $ do - -- Real tracing functions - PTrace.ptrace "foo" (pcon PUnit) `traces` ["foo"] - PTrace.ptrace "foo" (PTrace.ptrace "bar" $ pcon PUnit) `traces` ["foo", "bar"] - PTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` ["foo"] - PTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] - PTrace.ptraceIfTrue "foo" (PTrace.ptraceIfTrue "bar" $ pcon PTrue) `traces` ["bar", "foo"] - PTrace.ptraceIfTrue "foo" (PTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] - PTrace.ptraceIfFalse "foo" (PTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` ["foo"] - PTrace.ptrace "foo" (PTrace.ptraceIfTrue "bar" (pcon PTrue)) `traces` ["foo", "bar"] - PTrace.ptrace "foo" (PTrace.ptraceIfTrue "bar" (pcon PFalse)) `traces` ["foo"] - -- Dummy tracing functions - PNoTrace.ptrace "foo" (pcon PUnit) `traces` [] - PNoTrace.ptrace "foo" (PNoTrace.ptrace "bar" $ pcon PUnit) `traces` [] - PNoTrace.ptraceIfTrue "foo" (pcon PTrue) `traces` [] - PNoTrace.ptraceIfTrue "foo" (pcon PFalse) `traces` [] - PNoTrace.ptraceIfTrue "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PTrue) `traces` [] - PNoTrace.ptraceIfTrue "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] - PNoTrace.ptraceIfFalse "foo" (PNoTrace.ptraceIfTrue "bar" $ pcon PFalse) `traces` [] - PNoTrace.ptrace "foo" (PNoTrace.ptraceIfTrue "bar" (pcon PTrue)) `traces` [] - PNoTrace.ptrace "foo" (PNoTrace.ptraceIfTrue "bar" (pcon PFalse)) `traces` [] + , 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" $ diff --git a/examples/Plutarch/Spec/Tracing.hs b/examples/Plutarch/Spec/Tracing.hs new file mode 100644 index 000000000..9f9e47555 --- /dev/null +++ b/examples/Plutarch/Spec/Tracing.hs @@ -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 -} diff --git a/examples/examples.cabal b/examples/examples.cabal deleted file mode 100644 index 298582291..000000000 --- a/examples/examples.cabal +++ /dev/null @@ -1,88 +0,0 @@ -cabal-version: 3.0 -name: examples -version: 1.0.0 - -author: Las Safin -license: MIT - -test-suite examples - type: exitcode-stdio-1.0 - main-is: Main.hs - other-modules: - build-depends: - , base - , bytestring - , text - , plutarch - , plutarch-trace - , plutarch-trace:enable - , plutarch-trace:disable - , tasty - , tasty-hunit - , plutus-ledger-api - , plutus-core - , aeson - , plutus-tx - mixins: - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 - default-extensions: - DataKinds - DeriveAnyClass - DerivingStrategies - LambdaCase - TypeFamilies - OverloadedStrings - PartialTypeSignatures - ViewPatterns - -- poor man's GHC2021 - BangPatterns - BinaryLiterals - ConstrainedClassMethods - ConstraintKinds - DeriveDataTypeable - DeriveFoldable - DeriveFunctor - DeriveGeneric - DeriveLift - DeriveTraversable - DoAndIfThenElse - EmptyCase - EmptyDataDecls - EmptyDataDeriving - ExistentialQuantification - ExplicitForAll - FlexibleContexts - FlexibleInstances - ForeignFunctionInterface - GADTSyntax - GeneralisedNewtypeDeriving - HexFloatLiterals - ImplicitPrelude - ImportQualifiedPost - InstanceSigs - KindSignatures - MonomorphismRestriction - MultiParamTypeClasses - NamedFieldPuns - NamedWildCards - NumericUnderscores - PatternGuards - PolyKinds - PostfixOperators - RankNTypes - RelaxedPolyRec - ScopedTypeVariables - StandaloneDeriving - StandaloneKindSignatures - StarIsType - TraditionalRecordSyntax - TupleSections - TypeApplications - TypeOperators - TypeSynonymInstances - ghc-options: - -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures - -Wmissing-export-lists -Werror -Wincomplete-record-updates - -Wmissing-deriving-strategies -Wno-name-shadowing diff --git a/plutarch-trace/README.md b/plutarch-trace/README.md deleted file mode 100644 index e859ccb14..000000000 --- a/plutarch-trace/README.md +++ /dev/null @@ -1,194 +0,0 @@ -# plutarch-trace - -## Usage -You **need** `cabal-install >= 3.0` for this. Your package's cabal file **must** use a `cabal-version` of 3.0 or higher. - -* Add `plutarch-trace` to your `build-depends`. - -The next steps depend on whether or not you want tracing enabled. -### Enable Tracing -* Add `plutarch-trace:enable` to your `build-depends`. -* Add a mixin similar to the following- - - ```cabal - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - ``` - - This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with the real tracing functions. - - You can rename the `Plutarch.Trace` module if you want, to something else- - ```cabal - plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - ``` - This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. - -Example `.cabal` file- -```cabal -cabal-version: 3.0 -name: foo -version: 1.0.0 - -executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:enable - mixins: - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) - default-language: Haskell2010 -``` - -### Disable Tracing -* Add `plutarch-trace:disable` to your `build-depends`. -* Add a mixin similar to the following- - - ```cabal - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable), - ``` - - This makes the `Plutarch.Trace` module available for you to access in your code - and it sets it up with dummy functions that don't actually trace. - - You can rename the `Plutarch.Trace` module if you want, to something else- - ```cabal - plutarch-trace (Plutarch.Trace as FooBar) requires (Plutarch.TraceSig as Plutarch.Trace.Disable), - ``` - This makes it so you import `Plutarch.Trace` as `FooBar`. This *will* hide the `Plutarch.Trace` module, meaning you can only import it as `FooBar`. Otherwise it's the exact same as just importing `Plutarch.Trace` without renaming. - -Example `.cabal` file- -```cabal -cabal-version: 3.0 -name: foo -version: 1.0.0 - -executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:disable - mixins: - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 -``` - -### Both! -Yuo want both huh? Well you can do that too! You can import `Plutarch.Trace` with two different setups and alias them to two different names. -* Add both `plutarch-trace:enable` and `plutarch-trace:disable` to your `build-depends`. -* Add both mixins- - - ```cabal - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - ``` - - Here, we set the *tracing enabled* module as `Plutarch.Trace`, and rename the *tracing disabled* module as `Plutarch.NoTrace`. Of course, you need different names for them. So be sure to rename them using `as`. - -Example `.cabal` file- -```cabal -cabal-version: 3.0 -name: foo -version: 1.0.0 - -executable foo-exe - main-is: - Main.hs - build-depends: - base, - plutarch-trace, - plutarch-trace:enable, - plutarch-trace:disable - mixins: - plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable), - plutarch-trace (Plutarch.Trace as Plutarch.NoTrace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 -``` - -### Keep it generic -If you're a library author, you may want to leave the instantiation to the user of your library. In this case, you should simply add `plutarch-trace` as a dependency and use `Plutarch.Trace` in your code. Ignore other deps, ignore mixins. - -Library `.cabal` file- -```cabal -cabal-version: 3.0 -name: foo -version: 1.0.0 - -lib - hs-source-dirs: - src - exposed-modules: - Foo - Bar - build-depends: - base, - plutarch-trace - default-language: Haskell2010 -``` - -You won't be able to load your project into the repl with this though. There is no interface file for the abstract `Plutarch.Trace` module. That will only exist after instantiation. See [Common Issues #2](#common-issues). - -Users of your must add your library in their `build-depends`, alongside `plutarch-trace:enable` or `plutarch-trace:disable` (or both), alongside the appropriate mixins- -```cabal - mixins: - foo (Foo Bar) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) -``` - -> Note that it's *the `foo` library* that is instantiated with the concrete implementation. Every dependency that uses a generic signature needs to be instantiated like this. If there are more dependencies that are generic (e.g a `plutarch-trace` dependency), all of them need to be instantiated individually (unless you want generic use out of them and want to leave instantiating out to the next user above). - -`foo` is the package name that uses `plutarch-trace` in a generic way. `(Foo, Bar)` is a comma separated list of all the modules that you want to make visible from `foo`. If you don't want to rename any of the modules, you can actually just leave out the module list in parens and just write `foo requires ...` to make all modules visible with their original name. - -### Using flags to enable/disable tracing -One of the great things about backpack is that it interacts very well with cabal flags. You can keep your entire dependency hierarchy completely generic and only instantiate the roots of the depenedency hierarchy in your end-user executable/benchmark/test suite. While doing so, you might still want easy control over switching the tracing behavior. - -In this case, you can use a cabal flag to add the corresponding implementation dependency and mixins. Because backpack mixins are defined in the `.cabal` file, you can easily use `if` statements with a flag to control this- -```hs -cabal-version: 3.0 -name: foo -version: 1.0.0 - -flag development - description: Enable development mode (turns on tracing). - manual: True - default: True - -executable exm-exe - main-is: - Main.hs - build-depends: - base, - plutarch, - plutarch-trace, - if flag(development) - build-depends: plutarch-trace:enable - else - build-depends: plutarch-trace:disable - if flag(development) - mixins: plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Enable) - else - mixins: plutarch-trace (Plutarch.Trace) requires (Plutarch.TraceSig as Plutarch.Trace.Disable) - default-language: Haskell2010 -``` - -As you'd expect, you can use `cabal configure -f +development` and `cabal configure -f -development` to easily switch the tracing behaviors! - -### Hiding Backpack implementation details from your user -Here's *another* neat thing about backpack - if you want, you can totally hide the backpack implementation details and let your users use good ol' cabal flags instead of `mixins`. This is actually one of the usecases noted in the [cabal user guide](https://cabal.readthedocs.io/en/3.6/cabal-package.html#backpack) itself. -> You may also find it useful to use library:reexported-modules to reexport instantiated libraries to Backpack-unware users (e.g., Backpack can be used entirely as an implementation detail.) - -In essence, you would have a package that [instantiates both](#both) tracing behaviors as 2 modules and just re-exports them depending on cabal flags! - -## Contributing -### Learning Backpack -* [really-small-backpack-example](https://github.com/danidiaz/really-small-backpack-example) - - lesson2 is very useful to immediately get up and running! -* [cabal user guide for backpack](https://cabal.readthedocs.io/en/3.6/cabal-package.html#backpack) - - I think this was recently added. -* [backpack-str](https://github.com/haskell-backpack/backpack-str) - -### Common Issues -* Changed a few module names/structures and getting an error like "The library package ... does not require ..." or "The library package ... does not expose ..."? If you're sure you put the stuff in `signatures` and `exposed-modules` correctly - just do a `cabal clean` and rebuild. -* If GHCi reports that it cannot find interface files for the signatures when loading into `cabal repl` - it means whatever package you're loading into is using a generic signature and not instantiating it with any implementation. You cannot use the repl on these generic packages. You must instantiate it with a concrete impl in your `mixins`. diff --git a/plutarch-trace/impl/Plutarch/Trace/Disable.hs b/plutarch-trace/impl/Plutarch/Trace/Disable.hs deleted file mode 100644 index 28d746afb..000000000 --- a/plutarch-trace/impl/Plutarch/Trace/Disable.hs +++ /dev/null @@ -1,14 +0,0 @@ -module Plutarch.Trace.Disable (ptrace, ptraceIfTrue, ptraceIfFalse) where - -import Plutarch.Bool (PBool) -import Plutarch.Prelude -import Plutarch.String (PString) - -ptrace :: Term s PString -> Term s a -> Term s a -ptrace _ a = a - -ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool -ptraceIfTrue _ a = a - -ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool -ptraceIfFalse _ a = a diff --git a/plutarch-trace/plutarch-trace.cabal b/plutarch-trace/plutarch-trace.cabal deleted file mode 100644 index f53cbf057..000000000 --- a/plutarch-trace/plutarch-trace.cabal +++ /dev/null @@ -1,105 +0,0 @@ -cabal-version: 3.0 -name: plutarch-trace -version: 1.0.0 - -author: Las Safin , Chase -license: MIT - -extra-source-files: README.md - -common c - default-language: Haskell2010 - default-extensions: - DataKinds - DeriveAnyClass - DerivingStrategies - LambdaCase - TypeFamilies - OverloadedStrings - PartialTypeSignatures - -- poor man's GHC2021 - BangPatterns - BinaryLiterals - ConstrainedClassMethods - ConstraintKinds - DeriveDataTypeable - DeriveFoldable - DeriveFunctor - DeriveGeneric - DeriveLift - DeriveTraversable - DoAndIfThenElse - EmptyCase - EmptyDataDecls - EmptyDataDeriving - ExistentialQuantification - ExplicitForAll - FlexibleContexts - FlexibleInstances - ForeignFunctionInterface - GADTSyntax - GeneralisedNewtypeDeriving - HexFloatLiterals - ImplicitPrelude - ImportQualifiedPost - InstanceSigs - KindSignatures - MonomorphismRestriction - MultiParamTypeClasses - NamedFieldPuns - NamedWildCards - NumericUnderscores - PatternGuards - PolyKinds - PostfixOperators - RankNTypes - RelaxedPolyRec - ScopedTypeVariables - StandaloneDeriving - StandaloneKindSignatures - StarIsType - TraditionalRecordSyntax - TupleSections - TypeApplications - TypeOperators - TypeSynonymInstances - ghc-options: - -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures - -Wmissing-export-lists -Werror -Wincomplete-record-updates - -Wmissing-deriving-strategies -Wno-name-shadowing - -library - import: c - hs-source-dirs: src - signatures: - Plutarch.TraceSig - exposed-modules: - Plutarch.Trace - build-depends: - , base - , plutarch - -library enable - import: c - hs-source-dirs: - impl - visibility: - public - exposed-modules: - Plutarch.Trace.Enable - build-depends: - , base - , plutarch - , plutus-core - -library disable - import: c - hs-source-dirs: - impl - visibility: - public - exposed-modules: - Plutarch.Trace.Disable - build-depends: - , base - , plutarch diff --git a/plutarch-trace/src/Plutarch/Trace.hs b/plutarch-trace/src/Plutarch/Trace.hs deleted file mode 100644 index c09719bf5..000000000 --- a/plutarch-trace/src/Plutarch/Trace.hs +++ /dev/null @@ -1,3 +0,0 @@ -module Plutarch.Trace (module Plutarch.TraceSig) where - -import Plutarch.TraceSig diff --git a/plutarch-trace/src/Plutarch/TraceSig.hsig b/plutarch-trace/src/Plutarch/TraceSig.hsig deleted file mode 100644 index e5fbec980..000000000 --- a/plutarch-trace/src/Plutarch/TraceSig.hsig +++ /dev/null @@ -1,15 +0,0 @@ -signature Plutarch.TraceSig (ptrace, ptraceIfTrue, ptraceIfFalse) where - -import Prelude () -import Plutarch (Term) -import Plutarch.String (PString) -import Plutarch.Bool (PBool) - --- | Trace a message, then evaluate and return given argument. -ptrace :: Term s PString -> Term s a -> Term s a - --- | Trace a message if given argument evaluates to true. -ptraceIfTrue :: Term s PString -> Term s PBool -> Term s PBool - --- | Trace a message if given argument evaluates to false. -ptraceIfFalse :: Term s PString -> Term s PBool -> Term s PBool diff --git a/plutarch.cabal b/plutarch.cabal index 0015c9c07..8acca9a9c 100644 --- a/plutarch.cabal +++ b/plutarch.cabal @@ -7,37 +7,12 @@ license: MIT extra-source-files: README.md -library - exposed-modules: - Plutarch.Evaluate - Plutarch.Internal - Plutarch - Plutarch.Prelude - Plutarch.Pair - Plutarch.Rational - Plutarch.Either - Plutarch.Integer - Plutarch.Bool - Plutarch.String - Plutarch.ByteString - Plutarch.Builtin - Plutarch.DataRepr - Plutarch.ScriptContext - Plutarch.Maybe - Plutarch.Unit - Plutarch.Crypto - build-depends: - , base - , plutus-core - , plutus-ledger-api - , text - , hashable - , bytestring - , containers - , cryptonite - , plutus-tx - , mtl - , flat +flag development + description: Enable tracing functions within plutarch. + manual: True + default: False + +common c default-language: Haskell2010 default-extensions: DataKinds @@ -98,3 +73,62 @@ library -Wall -Wcompat -Wincomplete-uni-patterns -Wno-partial-type-signatures -Wmissing-export-lists -Werror -Wincomplete-record-updates -Wmissing-deriving-strategies -Wno-name-shadowing + +library + import: c + exposed-modules: + Plutarch.Evaluate + Plutarch.Internal + Plutarch + Plutarch.Prelude + Plutarch.Pair + Plutarch.Rational + Plutarch.Either + Plutarch.Integer + Plutarch.Bool + Plutarch.String + Plutarch.ByteString + Plutarch.Builtin + Plutarch.DataRepr + Plutarch.ScriptContext + Plutarch.Maybe + Plutarch.Unit + Plutarch.Crypto + Plutarch.Trace + build-depends: + , base + , plutus-core + , plutus-ledger-api + , text + , hashable + , bytestring + , containers + , cryptonite + , plutus-tx + , 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 From f959a1d2ddeb8aae40d84b5110f5815e46007939 Mon Sep 17 00:00:00 2001 From: Chase Date: Tue, 4 Jan 2022 14:47:08 +0530 Subject: [PATCH 23/23] Add `ptraceError` + some haddocks --- Plutarch/Trace.hs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Plutarch/Trace.hs b/Plutarch/Trace.hs index c0bc5a06a..fc818be7e 100644 --- a/Plutarch/Trace.hs +++ b/Plutarch/Trace.hs @@ -1,6 +1,6 @@ {-# LANGUAGE CPP #-} -module Plutarch.Trace (ptrace, ptraceIfTrue, ptraceIfFalse) where +module Plutarch.Trace (ptrace, ptraceIfTrue, ptraceIfFalse, ptraceError) where -- CPP support isn't great in fourmolu. {- ORMOLU_DISABLE -} @@ -25,6 +25,7 @@ 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 @@ -32,6 +33,15 @@ ptrace s a = pforce $ ptrace' # s # pdelay a 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 @@ -39,8 +49,8 @@ ptraceIfTrue s a' = plet a' $ \a -> pif a (ptrace' # s # a) a 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