Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Delegate Rational invariant to PNonZero + optimizations #435

Merged
merged 24 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bbba37d
Force rational invariant in `PlutusType` instance
TotallyNotChase Apr 13, 2022
77f56fe
Fix formatting
TotallyNotChase Apr 13, 2022
b52e4e4
Add `PNonZero`
TotallyNotChase Apr 14, 2022
8002d22
Rework `PRational` to use `PNonZero` + optimizations
TotallyNotChase Apr 14, 2022
6108d7c
Fix formatting
TotallyNotChase Apr 14, 2022
6de73b5
Merge branch 'staging' into rational-experiment
TotallyNotChase May 16, 2022
52476c4
Rename `pnonZero'` -> `ptryNonZero`
TotallyNotChase May 16, 2022
b07d97a
Deduplicate `Flip`
TotallyNotChase May 16, 2022
88916a0
`PTryFrom` instances for `PNonZero`
TotallyNotChase May 16, 2022
0ea3b7c
Unexport `PRational` constructor...
TotallyNotChase May 17, 2022
3ca3ef4
`TryFrom` instance for `PRational` and cleanup
TotallyNotChase May 17, 2022
2f66f92
`TryFrom` tests for `PRational`
TotallyNotChase May 17, 2022
c398e12
Merge branch 'staging' into rational-experiment
TotallyNotChase Jul 7, 2022
28edeec
Merge branch 'staging' into rational-experiment
TotallyNotChase Jul 7, 2022
b539f9b
Build fixes and update goldens
TotallyNotChase Jul 7, 2022
a385c8d
Fix formatting
TotallyNotChase Jul 7, 2022
0729fed
Actually export `pthrow`
TotallyNotChase Jul 12, 2022
cb73a12
Use `pthrow` for compilation error
TotallyNotChase Jul 12, 2022
03fe4ec
Don't use `punsafeCoerce` unless necessary
TotallyNotChase Jul 12, 2022
74b01ac
Fix formatting
TotallyNotChase Jul 12, 2022
8a5973e
Use `PPositive` rather than `PNonZero` for `PRational`
TotallyNotChase Jul 12, 2022
c99ec7e
Fix formatting
TotallyNotChase Jul 12, 2022
fa14b22
Address review comments
TotallyNotChase Jul 13, 2022
4e02da9
Merge branch 'staging' into rational-experiment
L-as Jul 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Plutarch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Plutarch (
PI.pforce,
PI.phoistAcyclic,
PI.plet,
PI.pthrow,
PI.Term,
PI.S,
PI.PType,
Expand Down
16 changes: 3 additions & 13 deletions Plutarch/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ module Plutarch.Builtin (
pchooseListBuiltin,
) where

import Data.Functor.Const (Const)
import Data.Proxy (Proxy (Proxy))
import GHC.Generics (Generic)
import Plutarch (
Expand Down Expand Up @@ -91,25 +92,14 @@ import Plutarch.List (
ptail,
)
import Plutarch.Show (PShow (pshow'), pshow)
import Plutarch.TermCont (TermCont (runTermCont), tcont, unTermCont)
import Plutarch.TryFrom (PSubtype, PTryFrom, PTryFromExcess, ptryFrom, ptryFrom', pupcast)
import Plutarch.Unit (PUnit)
import Plutarch.Unsafe (punsafeBuiltin, punsafeCoerce, punsafeDowncast)
import qualified PlutusCore as PLC
import PlutusTx (Data (Constr), ToData)
import qualified PlutusTx

import Plutarch.TermCont (TermCont (runTermCont), tcont, unTermCont)

import Data.Functor.Const (Const)

import Plutarch.TryFrom (
PSubtype,
PTryFrom,
PTryFromExcess,
ptryFrom,
ptryFrom',
pupcast,
)

-- | Plutus 'BuiltinPair'
data PBuiltinPair (a :: PType) (b :: PType) (s :: S) = PBuiltinPair (Term s (PBuiltinPair a b))

Expand Down
79 changes: 79 additions & 0 deletions Plutarch/Positive.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}

module Plutarch.Positive (PPositive, ppositive, ptryPositive) where

import Data.Functor.Const (Const)
import GHC.Generics (Generic)

import Plutarch.Bool (PEq, POrd, pif, (#<=))
import Plutarch.Builtin (PAsData, PData, PIsData, pdata)
import Plutarch.Integer (PInteger, PIntegral)

import Plutarch.Maybe (PMaybe (PJust, PNothing))

import Plutarch (
DerivePlutusType (DPTStrat),
PlutusType,
PlutusTypeNewtype,
Term,
TermCont (runTermCont),
pcon,
phoistAcyclic,
plam,
plet,
pthrow,
pto,
(#),
(#$),
type (:-->),
)
import Plutarch.Num (PNum (pfromInteger, (#-)))
import Plutarch.Show (PShow)
import Plutarch.TermCont (tcont)
import Plutarch.Trace (ptraceError)
import Plutarch.TryFrom (PTryFrom (PTryFromExcess, ptryFrom'), ptryFrom)

newtype PPositive s = PPositive (Term s PInteger)
deriving stock (Generic)
deriving anyclass (PlutusType, PIsData, PEq, POrd, PIntegral, PShow)
instance DerivePlutusType PPositive where type DPTStrat _ = PlutusTypeNewtype

instance PNum PPositive where
x #- y = ptryPositive #$ pto x #- pto y

pfromInteger x
| x <= 0 = pthrow "PPositive.pfromInteger: encountered non positive"
| otherwise = pcon $ PPositive $ pfromInteger x

instance PTryFrom PInteger PPositive where
type PTryFromExcess PInteger PPositive = Const ()
ptryFrom' opq = runTermCont $ pure (ptryPositive # opq, ())

newtype Flip f a b = Flip (f b a) deriving stock (Generic)

instance PTryFrom PData (PAsData PPositive) where
type PTryFromExcess PData (PAsData PPositive) = Flip Term PPositive
ptryFrom' opq = runTermCont $ do
(_, i) <- tcont $ ptryFrom @(PAsData PInteger) opq
res <- tcont . plet $ ptryPositive # i
resData <- tcont . plet $ pdata res
pure (resData, res)

-- | Build a 'PPositive' from a 'PInteger'. Yields 'PNothing' if argument is zero.
ppositive :: Term s (PInteger :--> PMaybe PPositive)
ppositive = phoistAcyclic $
plam $ \i ->
pif
(i #<= 0)
(pcon PNothing)
$ pcon . PJust . pcon $ PPositive i

-- | Partial version of 'PPositive'. Errors if argument is zero.
ptryPositive :: Term s (PInteger :--> PPositive)
ptryPositive = phoistAcyclic $
plam $ \i ->
pif
(i #<= 0)
(ptraceError "ptryPositive: building with non positive")
$ pcon $ PPositive i
4 changes: 2 additions & 2 deletions Plutarch/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Plutarch.Prelude (
pinl,
pto,
pfix,
pthrow,
Type,
S,
PType,
Expand All @@ -36,10 +37,9 @@ module Plutarch.Prelude (
PIntegral (pdiv, pmod, pquot, prem),

-- * Rational numbers and utilities
PRational,
PRational (PRational),
pnumerator,
pdenominator,
pfromInteger,
pround,

-- * Booleans and boolean functions
Expand Down
Loading