-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use
PPositive
rather than PNonZero
for PRational
- Loading branch information
1 parent
74b01ac
commit 18234e5
Showing
10 changed files
with
141 additions
and
141 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
{-# 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 0 = pthrow "PPositive.pfromInteger: encountered 0" | ||
pfromInteger x = 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.