Skip to content

Commit

Permalink
new deps
Browse files Browse the repository at this point in the history
  • Loading branch information
colll78 committed Jan 22, 2025
1 parent c3799ed commit 92e3f82
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 76 deletions.
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ packages:
source-repository-package
type: git
location: https://github.com/Plutonomicon/plutarch-plutus
tag: 0a548108f9d83e4830a1a3ae1e58e61856c1bc42
tag: 8cc0a4ca3ed494a79fe019c72173b369cd26cb84
subdir:
.
plutarch-ledger-api
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
};

plutarch = {
url = "github:Plutonomicon/plutarch-plutus/0a548108f9d83e4830a1a3ae1e58e61856c1bc42";
url = "github:Plutonomicon/plutarch-plutus/8cc0a4ca3ed494a79fe019c72173b369cd26cb84";
};
};

Expand Down
2 changes: 1 addition & 1 deletion nix/project.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

let
sha256map = {
"https://github.com/Plutonomicon/plutarch-plutus"."0a548108f9d83e4830a1a3ae1e58e61856c1bc42" = "sha256-n+L9BM4nb0KtmBme5fSBzjB1s0AYMwB383Kz6UbEXEY=";
"https://github.com/Plutonomicon/plutarch-plutus"."8cc0a4ca3ed494a79fe019c72173b369cd26cb84" = "sha256-zQ0qv9e3Gy6Myn8wx+geJtCL5L2qJlpruSbWmrgyLOE=";
};

modules = [{ }];
Expand Down
58 changes: 8 additions & 50 deletions src/plutarch-onchain-lib/lib/Plutarch/Core/Unroll.hs
Original file line number Diff line number Diff line change
@@ -1,53 +1,11 @@
{- |
Unrolling a recursive function involves explicitly laying out some or all of the recursive steps, rather than relying on recursion via a fixed-point combinator.
In UPLC, the typical `pfix` implementation uses a Y-combinator under the hood. Each recursive step incurs additional evaluation costs (CPU and memory) due to
the execution of the Y-combinator. By eliminating these costs in each recursive step, unrolled functions reduce execution overhead. However, since each recursive
step is explicitly laid out, unrolled functions consume more script size.
There are various unrolling strategies available. It is important to carefully study the implications of each strategy, as they may impose different requirements,
such as hard limit on recursion depth.
-}
module Plutarch.Core.Unroll (punrollBound, punrollUnbound, punrollUnboundWhole) where

import Plutarch.Internal.Fix (pfix)
import Plutarch.Internal.PLam (plam)
import Plutarch.Internal.Term (Term, (#$), (:-->))

{- |
The first argument specifies the unrolling depth.
The second argument defines the fallback behavior when the recursion depth exceeds the provided unrolling depth.
The fixed-point implementation provided requires a Haskell-level value @c@ and a Plutarch function of type `Term s (a :--> b)`. The functional for the recursion is passed as a Haskell function.
The inclusion of the additional, arbitrary Haskell value (typed @c@) enables further optimization by allowing pre-computation of constant values that depend only on the recursion depth.
@since WIP
{-|
Module : Plutarch.Core.Unroll
Description : Collection of plutarch metaprogramming utilities for unrolling recursive functions
Copyright : (c) Philip DiSarro, 2024
Stability : experimental
-}
punrollBound ::
forall a b c s.
Integer ->
(c -> Term s (a :--> b)) ->
((c -> Term s (a :--> b)) -> c -> Term s (a :--> b)) ->
c ->
Term s (a :--> b)
punrollBound 0 def _ c = def c
punrollBound d def f c = f (punrollBound (d - 1) def f) c

{- |
Unroll given amount of steps, and for rest, uses `pfix` to support unbound recursion.
module Plutarch.Core.Unroll (punrollBound', punrollBound, punrollUnbound, punrollUnboundWhole) where

@since WIP
-}
punrollUnbound :: forall a b s. Integer -> (Term s (a :--> b) -> Term s (a :--> b)) -> Term s (a :--> b)
punrollUnbound 0 f = pfix #$ plam f
punrollUnbound d f = f (punrollUnbound (d - 1) f)

{- |
Uses `pfix` to recurse unrolled function itself. Unlike @punrollUnbound@, this function uses unrolled instructions
within `pfix` recursions.
This should perform better than @punrollUnbound@ when a function requires a large recursion depth.
@since WIP
-}
punrollUnboundWhole :: forall a b s. Integer -> (Term s (a :--> b) -> Term s (a :--> b)) -> Term s (a :--> b)
punrollUnboundWhole d f = pfix #$ plam $ \r -> punrollBound d (const r) (\g () -> f (g ())) ()
import Plutarch.Unroll (punrollBound, punrollBound', punrollUnbound,
punrollUnboundWhole)
Original file line number Diff line number Diff line change
Expand Up @@ -73,23 +73,12 @@ instance DerivePlutusType PMerklePatriciaForestry where type DPTStrat _ = Plutus

-- TODO:
-- Fix this in Plutarch so that we can use PLiftable for types with BuiltinByteString fields.
-- instance
-- PLiftable PMerklePatriciaForestry
-- where
-- type AsHaskell PMerklePatriciaForestry = BuiltinByteString
-- type PlutusRepr PMerklePatriciaForestry = PLiftedClosed PMerklePatriciaForestry

instance {-# OVERLAPPING #-} PLiftable PMerklePatriciaForestry where
type AsHaskell PMerklePatriciaForestry = AsHaskell PByteString
type PlutusRepr PMerklePatriciaForestry = PlutusTx.Data
{-# INLINEABLE toPlutarchRepr #-}
toPlutarchRepr = PlutusTx.toData . BuiltinByteString
{-# INLINEABLE toPlutarch #-}
toPlutarch = toPlutarchUni
{-# INLINEABLE fromPlutarchRepr #-}
fromPlutarchRepr x = (\(BuiltinByteString str) -> str) <$> PlutusTx.fromData x
{-# INLINEABLE fromPlutarch #-}
fromPlutarch = fromPlutarchUni
instance
PLiftable PMerklePatriciaForestry
where
type AsHaskell PMerklePatriciaForestry = BuiltinByteString
type PlutusRepr PMerklePatriciaForestry = PLiftedClosed PMerklePatriciaForestry


-- TODO:
-- Fix this in Plutarch so that we can use PLiftable for types with BuiltinByteString fields.
Expand Down
4 changes: 2 additions & 2 deletions src/plutarch-onchain-lib/test/Bench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ elemAtBenches =
]

unrollLengthBound :: forall list a s. PIsListLike list a => Term s (list a :--> PInteger)
unrollLengthBound = punrollBound 200 (const $ plam $ \_ -> pconstant (-1)) go 0
unrollLengthBound = punrollBound' 200 (const $ plam $ \_ -> pconstant (-1)) go 0
where
go ::
(Integer -> Term s (list a :--> PInteger)) ->
Expand All @@ -149,7 +149,7 @@ unrollLengthBound = punrollBound 200 (const $ plam $ \_ -> pconstant (-1)) go 0
go self n = plam $ pelimList (\_ xs -> self (n + 1) # xs) (pconstant n)

punrolledCountScriptInputs :: Term s (PBuiltinList PTxInInfo :--> PInteger)
punrolledCountScriptInputs = punrollBound 100 (const def) go () # 0
punrolledCountScriptInputs = punrollBound' 100 (const def) go () # 0
where
def :: Term s (PInteger :--> PBuiltinList PTxInInfo :--> PInteger)
def = plam $ \_ _ -> -1
Expand Down

0 comments on commit 92e3f82

Please sign in to comment.