{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

{- | Value-related functionality. In order to keep the interface efficient and
 safe at the same time, there is a type-level distinction between 'PValue's
 that are guaranteed to be properly normalized and those that provide no
 such guarantee.

 Also for efficiency reasons, the Ada-specific functions assume that there
 can be only one token name for the Ada currency symbol, and they don't check
 whether it matches 'Plutus.adaToken'.
-}
module Plutarch.Api.V1.Value (
  PValue (PValue),
  PCurrencySymbol (PCurrencySymbol),
  PTokenName (PTokenName),
  KeyGuarantees (Unsorted, Sorted),
  AmountGuarantees (NoGuarantees, NonZero, Positive),

  -- * Conversions and assertions
  passertSorted,
  passertPositive,
  pforgetPositive,
  pforgetSorted,
  pnormalize,

  -- * Creation
  psingleton,
  psingletonData,
  pconstantSingleton,
  pconstantPositiveSingleton,

  -- * Combining values
  punionWith,
  punionWithData,

  -- * Partial ordering operations
  pcheckBinRel,

  -- * Lookups
  pvalueOf,
  plovelaceValueOf,

  -- * Ada-specific
  padaSymbol,
  padaSymbolData,
  padaToken,
  padaTokenData,
  pisAdaOnlyValue,
  padaOnlyValue,
  pnoAdaValue,
) where

import PlutusLedgerApi.V1 qualified as Plutus

import Plutarch.Api.V1.AssocMap (KeyGuarantees (Sorted, Unsorted), PMap (..))
import Plutarch.Api.V1.AssocMap qualified as AssocMap
import Plutarch.Bool (pand', pif')
import Plutarch.Lift (
  DerivePConstantViaBuiltin (DerivePConstantViaBuiltin),
  DerivePConstantViaNewtype (DerivePConstantViaNewtype),
  PConstantDecl,
  PLifted,
  PUnsafeLiftDecl,
 )
import Plutarch.List qualified as List
import Plutarch.TryFrom (PTryFrom (PTryFromExcess, ptryFrom'))
import Plutarch.Unsafe (punsafeCoerce, punsafeDowncast)
import PlutusTx.Monoid qualified as PlutusTx
import PlutusTx.Semigroup qualified as PlutusTx

import Plutarch.Prelude hiding (psingleton)

newtype Flip f a b = Flip (f b a) deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k k (f :: k -> k -> Type) (a :: k) (b :: k) x.
Rep (Flip @k @k f a b) x -> Flip @k @k f a b
forall k k (f :: k -> k -> Type) (a :: k) (b :: k) x.
Flip @k @k f a b -> Rep (Flip @k @k f a b) x
$cto :: forall k k (f :: k -> k -> Type) (a :: k) (b :: k) x.
Rep (Flip @k @k f a b) x -> Flip @k @k f a b
$cfrom :: forall k k (f :: k -> k -> Type) (a :: k) (b :: k) x.
Flip @k @k f a b -> Rep (Flip @k @k f a b) x
Generic)

newtype PTokenName (s :: S) = PTokenName (Term s PByteString)
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PTokenName s) x -> PTokenName s
forall (s :: S) x. PTokenName s -> Rep (PTokenName s) x
$cto :: forall (s :: S) x. Rep (PTokenName s) x -> PTokenName s
$cfrom :: forall (s :: S) x. PTokenName s -> Rep (PTokenName s) x
Generic)
  deriving anyclass (forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
forall (s :: S) (b :: PType).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
$cpmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PTokenName)
-> (PTokenName s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
$cpcon' :: forall (s :: S). PTokenName s -> Term s (PInner PTokenName)
PlutusType, forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
forall (s :: S). Term s PTokenName -> Term s PData
forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
pdataImpl :: forall (s :: S). Term s PTokenName -> Term s PData
$cpdataImpl :: forall (s :: S). Term s PTokenName -> Term s PData
pfromDataImpl :: forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
$cpfromDataImpl :: forall (s :: S). Term s (PAsData PTokenName) -> Term s PTokenName
PIsData, forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
#== :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
$c#== :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
PEq, PEq PTokenName
forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> PPartialOrd t
#< :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
$c#< :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
#<= :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
$c#<= :: forall (s :: S).
Term s PTokenName -> Term s PTokenName -> Term s PBool
PPartialOrd, PPartialOrd PTokenName
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (s :: S). Bool -> Term s PTokenName -> Term s PString
forall (t :: PType).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
pshow' :: forall (s :: S). Bool -> Term s PTokenName -> Term s PString
$cpshow' :: forall (s :: S). Bool -> Term s PTokenName -> Term s PString
PShow)
instance DerivePlutusType PTokenName where type DPTStrat _ = PlutusTypeNewtype

instance PUnsafeLiftDecl PTokenName where type PLifted PTokenName = Plutus.TokenName
deriving via
  (DerivePConstantViaBuiltin Plutus.TokenName PTokenName PByteString)
  instance
    PConstantDecl Plutus.TokenName

instance PTryFrom PData (PAsData PTokenName) where
  type PTryFromExcess PData (PAsData PTokenName) = Flip Term PTokenName
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData PTokenName),
     Reduce (PTryFromExcess PData (PAsData PTokenName) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    Term s PByteString
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PByteString) Term s PData
opq forall a b. (a, b) -> b
snd
    forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ \() -> Term s r
f ->
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (forall (s :: S). Term s (PByteString :--> PInteger)
plengthBS forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PByteString
unwrapped forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
#<= Term s PInteger
32) (() -> Term s r
f ()) (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"ptryFrom(TokenName): must be at most 32 Bytes long")
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S). Term s PByteString -> PTokenName s
PTokenName forall a b. (a -> b) -> a -> b
$ Term s PByteString
unwrapped)

newtype PCurrencySymbol (s :: S) = PCurrencySymbol (Term s PByteString)
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PCurrencySymbol s) x -> PCurrencySymbol s
forall (s :: S) x. PCurrencySymbol s -> Rep (PCurrencySymbol s) x
$cto :: forall (s :: S) x. Rep (PCurrencySymbol s) x -> PCurrencySymbol s
$cfrom :: forall (s :: S) x. PCurrencySymbol s -> Rep (PCurrencySymbol s) x
Generic)
  deriving anyclass (forall (s :: S).
PCurrencySymbol s -> Term s (PInner PCurrencySymbol)
forall (s :: S) (b :: PType).
Term s (PInner PCurrencySymbol)
-> (PCurrencySymbol s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PCurrencySymbol)
-> (PCurrencySymbol s -> Term s b) -> Term s b
$cpmatch' :: forall (s :: S) (b :: PType).
Term s (PInner PCurrencySymbol)
-> (PCurrencySymbol s -> Term s b) -> Term s b
pcon' :: forall (s :: S).
PCurrencySymbol s -> Term s (PInner PCurrencySymbol)
$cpcon' :: forall (s :: S).
PCurrencySymbol s -> Term s (PInner PCurrencySymbol)
PlutusType, forall (s :: S).
Term s (PAsData PCurrencySymbol) -> Term s PCurrencySymbol
forall (s :: S). Term s PCurrencySymbol -> Term s PData
forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
pdataImpl :: forall (s :: S). Term s PCurrencySymbol -> Term s PData
$cpdataImpl :: forall (s :: S). Term s PCurrencySymbol -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData PCurrencySymbol) -> Term s PCurrencySymbol
$cpfromDataImpl :: forall (s :: S).
Term s (PAsData PCurrencySymbol) -> Term s PCurrencySymbol
PIsData, forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
#== :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
$c#== :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
PEq, PEq PCurrencySymbol
forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> (forall (s :: S). Term s t -> Term s t -> Term s PBool)
-> PPartialOrd t
#< :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
$c#< :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
#<= :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
$c#<= :: forall (s :: S).
Term s PCurrencySymbol -> Term s PCurrencySymbol -> Term s PBool
PPartialOrd, PPartialOrd PCurrencySymbol
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (s :: S). Bool -> Term s PCurrencySymbol -> Term s PString
forall (t :: PType).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
pshow' :: forall (s :: S). Bool -> Term s PCurrencySymbol -> Term s PString
$cpshow' :: forall (s :: S). Bool -> Term s PCurrencySymbol -> Term s PString
PShow)
instance DerivePlutusType PCurrencySymbol where type DPTStrat _ = PlutusTypeNewtype

instance PTryFrom PData (PAsData PCurrencySymbol) where
  type PTryFromExcess PData (PAsData PCurrencySymbol) = Flip Term PCurrencySymbol
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData PCurrencySymbol),
     Reduce (PTryFromExcess PData (PAsData PCurrencySymbol) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    Term s PByteString
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PByteString) Term s PData
opq forall a b. (a, b) -> b
snd
    Term s PInteger
len <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall a b. (a -> b) -> a -> b
$ forall (s :: S). Term s (PByteString :--> PInteger)
plengthBS forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PByteString
unwrapped
    forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ \() -> Term s r
f ->
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (Term s PInteger
len forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
0 forall (s :: S). Term s PBool -> Term s PBool -> Term s PBool
#|| Term s PInteger
len forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
28) (() -> Term s r
f ()) (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"ptryFrom(CurrencySymbol): must be 28 bytes long or empty")
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S). Term s PByteString -> PCurrencySymbol s
PCurrencySymbol forall a b. (a -> b) -> a -> b
$ Term s PByteString
unwrapped)

instance PUnsafeLiftDecl PCurrencySymbol where type PLifted PCurrencySymbol = Plutus.CurrencySymbol
deriving via
  (DerivePConstantViaBuiltin Plutus.CurrencySymbol PCurrencySymbol PByteString)
  instance
    PConstantDecl Plutus.CurrencySymbol

data AmountGuarantees = NoGuarantees | NonZero | Positive

type role PValue nominal nominal nominal
newtype PValue (keys :: KeyGuarantees) (amounts :: AmountGuarantees) (s :: S)
  = PValue (Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger)))
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) x.
Rep (PValue keys amounts s) x -> PValue keys amounts s
forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) x.
PValue keys amounts s -> Rep (PValue keys amounts s) x
$cto :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) x.
Rep (PValue keys amounts s) x -> PValue keys amounts s
$cfrom :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) x.
PValue keys amounts s -> Rep (PValue keys amounts s) x
Generic)
  deriving anyclass (forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
PValue keys amounts s -> Term s (PInner (PValue keys amounts))
forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) (b :: PType).
Term s (PInner (PValue keys amounts))
-> (PValue keys amounts s -> Term s b) -> Term s b
forall (a :: PType).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: PType).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PValue keys amounts))
-> (PValue keys amounts s -> Term s b) -> Term s b
$cpmatch' :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S) (b :: PType).
Term s (PInner (PValue keys amounts))
-> (PValue keys amounts s -> Term s b) -> Term s b
pcon' :: forall (s :: S).
PValue keys amounts s -> Term s (PInner (PValue keys amounts))
$cpcon' :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
PValue keys amounts s -> Term s (PInner (PValue keys amounts))
PlutusType, forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PAsData (PValue keys amounts))
-> Term s (PValue keys amounts)
forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PValue keys amounts) -> Term s PData
forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
pdataImpl :: forall (s :: S). Term s (PValue keys amounts) -> Term s PData
$cpdataImpl :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PValue keys amounts) -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData (PValue keys amounts))
-> Term s (PValue keys amounts)
$cpfromDataImpl :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PAsData (PValue keys amounts))
-> Term s (PValue keys amounts)
PIsData, forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Bool -> Term s (PValue keys amounts) -> Term s PString
forall (t :: PType).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
pshow' :: forall (s :: S).
Bool -> Term s (PValue keys amounts) -> Term s PString
$cpshow' :: forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Bool -> Term s (PValue keys amounts) -> Term s PString
PShow)
instance DerivePlutusType (PValue keys amounts) where type DPTStrat _ = PlutusTypeNewtype

instance PUnsafeLiftDecl (PValue 'Unsorted 'NonZero) where
  type PLifted (PValue 'Unsorted 'NonZero) = Plutus.Value
deriving via
  ( DerivePConstantViaNewtype
      Plutus.Value
      (PValue 'Unsorted 'NonZero)
      (PMap 'Unsorted PCurrencySymbol (PMap 'Unsorted PTokenName PInteger))
  )
  instance
    PConstantDecl Plutus.Value

instance PEq (PValue 'Sorted 'Positive) where
  Term s (PValue 'Sorted 'Positive)
a #== :: forall (s :: S).
Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive) -> Term s PBool
#== Term s (PValue 'Sorted 'Positive)
b = forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted 'Positive)
a forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted 'Positive)
b

instance PEq (PValue 'Sorted 'NonZero) where
  Term s (PValue 'Sorted 'NonZero)
a #== :: forall (s :: S).
Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero) -> Term s PBool
#== Term s (PValue 'Sorted 'NonZero)
b = forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted 'NonZero)
a forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted 'NonZero)
b

{- | Partial ordering implementation for sorted 'PValue' with 'Positive' amounts.

Use 'pcheckBinRel' if 'AmountGuarantees' is 'NoGuarantees'.
-}
instance PPartialOrd (PValue 'Sorted 'Positive) where
  Term s (PValue 'Sorted 'Positive)
a #< :: forall (s :: S).
Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive) -> Term s PBool
#< Term s (PValue 'Sorted 'Positive)
b = Term s (PValue 'Sorted 'NonZero)
a' forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
#< forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive Term s (PValue 'Sorted 'Positive)
b
    where
      a' :: Term s (PValue 'Sorted 'NonZero)
a' = forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive Term s (PValue 'Sorted 'Positive)
a :: Term _ (PValue 'Sorted 'NonZero)
  Term s (PValue 'Sorted 'Positive)
a #<= :: forall (s :: S).
Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive) -> Term s PBool
#<= Term s (PValue 'Sorted 'Positive)
b = Term s (PValue 'Sorted 'NonZero)
a' forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
#<= forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive Term s (PValue 'Sorted 'Positive)
b
    where
      a' :: Term s (PValue 'Sorted 'NonZero)
a' = forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive Term s (PValue 'Sorted 'Positive)
a :: Term _ (PValue 'Sorted 'NonZero)

{- | Partial ordering implementation for sorted 'PValue' with 'NonZero' amounts.

Use 'pcheckBinRel' if 'AmountGuarantees' is 'NoGuarantees'.
-}
instance PPartialOrd (PValue 'Sorted 'NonZero) where
  Term s (PValue 'Sorted 'NonZero)
a #< :: forall (s :: S).
Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero) -> Term s PBool
#< Term s (PValue 'Sorted 'NonZero)
b = forall {s :: S} {any0 :: AmountGuarantees}
       {any1 :: AmountGuarantees}.
Term s (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool))
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
b
    where
      f :: Term s (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool))
f = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PBool))
   :--> (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool)))
pcheckBinRel forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$ forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
(#<)
  Term s (PValue 'Sorted 'NonZero)
a #<= :: forall (s :: S).
Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero) -> Term s PBool
#<= Term s (PValue 'Sorted 'NonZero)
b = forall {s :: S} {any0 :: AmountGuarantees}
       {any1 :: AmountGuarantees}.
Term s (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool))
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
b
    where
      f :: Term s (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool))
f = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PBool))
   :--> (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool)))
pcheckBinRel forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$ forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
(#<=)

instance PEq (PValue 'Sorted 'NoGuarantees) where
  Term s (PValue 'Sorted 'NoGuarantees)
a #== :: forall (s :: S).
Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees) -> Term s PBool
#== Term s (PValue 'Sorted 'NoGuarantees)
b = forall (v :: PType) (s :: S) (any :: KeyGuarantees) (k :: PType).
PIsData v =>
Term s ((v :--> PBool) :--> (PMap any k v :--> PBool))
AssocMap.pall forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (v :: PType) (s :: S) (any :: KeyGuarantees) (k :: PType).
PIsData v =>
Term s ((v :--> PBool) :--> (PMap any k v :--> PBool))
AssocMap.pall forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam (forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
0)) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto (forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(-) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
b)

instance Semigroup (Term s (PValue 'Sorted 'Positive)) where
  Term s (PValue 'Sorted 'Positive)
a <> :: Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive)
<> Term s (PValue 'Sorted 'Positive)
b = forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'Positive)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'Positive)
b)

instance PlutusTx.Semigroup (Term s (PValue 'Sorted 'Positive)) where
  Term s (PValue 'Sorted 'Positive)
a <> :: Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive)
-> Term s (PValue 'Sorted 'Positive)
<> Term s (PValue 'Sorted 'Positive)
b = forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'Positive)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'Positive)
b)

instance Semigroup (Term s (PValue 'Sorted 'NonZero)) where
  Term s (PValue 'Sorted 'NonZero)
a <> :: Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero)
<> Term s (PValue 'Sorted 'NonZero)
b = forall (s :: S) (any :: AmountGuarantees).
Term s (PValue 'Sorted any :--> PValue 'Sorted 'NonZero)
pnormalize forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
b

instance PlutusTx.Semigroup (Term s (PValue 'Sorted 'NonZero)) where
  Term s (PValue 'Sorted 'NonZero)
a <> :: Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero)
<> Term s (PValue 'Sorted 'NonZero)
b = forall (s :: S) (any :: AmountGuarantees).
Term s (PValue 'Sorted any :--> PValue 'Sorted 'NonZero)
pnormalize forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NonZero)
b

instance Semigroup (Term s (PValue 'Sorted 'NoGuarantees)) where
  Term s (PValue 'Sorted 'NoGuarantees)
a <> :: Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees)
<> Term s (PValue 'Sorted 'NoGuarantees)
b = forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
b

instance PlutusTx.Semigroup (Term s (PValue 'Sorted 'NoGuarantees)) where
  Term s (PValue 'Sorted 'NoGuarantees)
a <> :: Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees)
<> Term s (PValue 'Sorted 'NoGuarantees)
b = forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a -> a
(+) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
b

instance
  Semigroup (Term s (PValue 'Sorted normalization)) =>
  Monoid (Term s (PValue 'Sorted normalization))
  where
  mempty :: Term s (PValue 'Sorted normalization)
mempty = forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall (s :: S) (k :: PType) (v :: PType).
Term s (PMap 'Sorted k v)
AssocMap.pempty)

instance
  PlutusTx.Semigroup (Term s (PValue 'Sorted normalization)) =>
  PlutusTx.Monoid (Term s (PValue 'Sorted normalization))
  where
  mempty :: Term s (PValue 'Sorted normalization)
mempty = forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall (s :: S) (k :: PType) (v :: PType).
Term s (PMap 'Sorted k v)
AssocMap.pempty)

instance
  PlutusTx.Semigroup (Term s (PValue 'Sorted 'NoGuarantees)) =>
  PlutusTx.Group (Term s (PValue 'Sorted 'NoGuarantees))
  where
  inv :: Term s (PValue 'Sorted 'NoGuarantees)
-> Term s (PValue 'Sorted 'NoGuarantees)
inv Term s (PValue 'Sorted 'NoGuarantees)
a = forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term
  s
  ((PInteger :--> PInteger)
   :--> (PValue k a :--> PValue k 'NoGuarantees))
pmapAmounts forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a. Num a => a -> a
negate forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted 'NoGuarantees)
a

instance
  PlutusTx.Semigroup (Term s (PValue 'Sorted 'NonZero)) =>
  PlutusTx.Group (Term s (PValue 'Sorted 'NonZero))
  where
  inv :: Term s (PValue 'Sorted 'NonZero)
-> Term s (PValue 'Sorted 'NonZero)
inv Term s (PValue 'Sorted 'NonZero)
a = forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce forall a b. (a -> b) -> a -> b
$ forall a. Group a => a -> a
PlutusTx.inv (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s (PValue 'Sorted 'NonZero)
a :: Term s (PValue 'Sorted 'NoGuarantees))

instance PTryFrom PData (PAsData (PValue 'Unsorted 'NoGuarantees))
instance PTryFrom PData (PAsData (PValue 'Sorted 'NoGuarantees))

instance PTryFrom PData (PAsData (PValue 'Sorted 'Positive)) where
  type PTryFromExcess PData (PAsData (PValue 'Sorted 'Positive)) = Flip Term (PValue 'Sorted 'Positive)
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData (PValue 'Sorted 'Positive)),
     Reduce
       (PTryFromExcess PData (PAsData (PValue 'Sorted 'Positive)) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    (Term s (PAsData (PValue 'Sorted 'NoGuarantees))
opq', Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
_) <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData (PValue 'Sorted 'NoGuarantees)) Term s PData
opq
    Term s (PValue 'Sorted 'Positive)
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
papp forall (kg :: KeyGuarantees) (ag :: AmountGuarantees) (s :: S).
Term s (PValue kg ag :--> PValue kg 'Positive)
passertPositive forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData forall a b. (a -> b) -> a -> b
$ Term s (PAsData (PValue 'Sorted 'NoGuarantees))
opq'
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, Term s (PValue 'Sorted 'Positive)
unwrapped)

instance PTryFrom PData (PAsData (PValue 'Unsorted 'Positive)) where
  type PTryFromExcess PData (PAsData (PValue 'Unsorted 'Positive)) = Flip Term (PValue 'Unsorted 'Positive)
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData (PValue 'Unsorted 'Positive)),
     Reduce
       (PTryFromExcess PData (PAsData (PValue 'Unsorted 'Positive)) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    (Term s (PAsData (PValue 'Unsorted 'NoGuarantees))
opq', Term
  s
  (PMap
     'Unsorted PCurrencySymbol (PMap 'Unsorted PTokenName PInteger))
_) <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData (PValue 'Unsorted 'NoGuarantees)) Term s PData
opq
    Term s (PValue 'Unsorted 'Positive)
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
papp forall (kg :: KeyGuarantees) (ag :: AmountGuarantees) (s :: S).
Term s (PValue kg ag :--> PValue kg 'Positive)
passertPositive forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData forall a b. (a -> b) -> a -> b
$ Term s (PAsData (PValue 'Unsorted 'NoGuarantees))
opq'
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, Term s (PValue 'Unsorted 'Positive)
unwrapped)

instance PTryFrom PData (PAsData (PValue 'Sorted 'NonZero)) where
  type PTryFromExcess PData (PAsData (PValue 'Sorted 'NonZero)) = Flip Term (PValue 'Sorted 'NonZero)
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData (PValue 'Sorted 'NonZero)),
     Reduce
       (PTryFromExcess PData (PAsData (PValue 'Sorted 'NonZero)) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    (Term s (PAsData (PValue 'Sorted 'NoGuarantees))
opq', Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
_) <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData (PValue 'Sorted 'NoGuarantees)) Term s PData
opq
    Term s (PValue 'Sorted 'NonZero)
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
papp forall (kg :: KeyGuarantees) (ag :: AmountGuarantees).
ClosedTerm (PValue kg ag :--> PValue kg 'NonZero)
passertNonZero forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData forall a b. (a -> b) -> a -> b
$ Term s (PAsData (PValue 'Sorted 'NoGuarantees))
opq'
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, Term s (PValue 'Sorted 'NonZero)
unwrapped)

instance PTryFrom PData (PAsData (PValue 'Unsorted 'NonZero)) where
  type PTryFromExcess PData (PAsData (PValue 'Unsorted 'NonZero)) = Flip Term (PValue 'Unsorted 'NonZero)
  ptryFrom' :: forall (s :: S) (r :: PType).
Term s PData
-> ((Term s (PAsData (PValue 'Unsorted 'NonZero)),
     Reduce
       (PTryFromExcess PData (PAsData (PValue 'Unsorted 'NonZero)) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = forall (r :: PType) (s :: S) a.
TermCont @r s a -> (a -> Term s r) -> Term s r
runTermCont forall a b. (a -> b) -> a -> b
$ do
    (Term s (PAsData (PValue 'Unsorted 'NoGuarantees))
opq', Term
  s
  (PMap
     'Unsorted PCurrencySymbol (PMap 'Unsorted PTokenName PInteger))
_) <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall a b. (a -> b) -> a -> b
$ forall (b :: PType) (a :: PType) (s :: S) (r :: PType).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData (PValue 'Unsorted 'NoGuarantees)) Term s PData
opq
    Term s (PValue 'Unsorted 'NonZero)
unwrapped <- forall a (s :: S) (r :: PType).
((a -> Term s r) -> Term s r) -> TermCont @r s a
tcont forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
papp forall (kg :: KeyGuarantees) (ag :: AmountGuarantees).
ClosedTerm (PValue kg ag :--> PValue kg 'NonZero)
passertNonZero forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData forall a b. (a -> b) -> a -> b
$ Term s (PAsData (PValue 'Unsorted 'NoGuarantees))
opq'
    forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s PData
opq, Term s (PValue 'Unsorted 'NonZero)
unwrapped)

-- | Construct a constant singleton 'PValue' containing only the given quantity of the given currency.
pconstantSingleton ::
  ClosedTerm PCurrencySymbol ->
  ClosedTerm PTokenName ->
  ClosedTerm PInteger ->
  ClosedTerm (PValue 'Sorted 'NonZero)
pconstantSingleton :: ClosedTerm PCurrencySymbol
-> ClosedTerm PTokenName
-> ClosedTerm PInteger
-> ClosedTerm (PValue 'Sorted 'NonZero)
pconstantSingleton ClosedTerm PCurrencySymbol
symbol ClosedTerm PTokenName
token ClosedTerm PInteger
amount
  | forall (p :: PType).
(HasCallStack, PLift p) =>
ClosedTerm p -> PLifted p
plift ClosedTerm PInteger
amount forall a. Eq a => a -> a -> Bool
== PLifted PInteger
0 = forall a. Monoid a => a
mempty
  | Bool
otherwise = forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast (forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PCurrencySymbol
symbol forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PTokenName
token forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PInteger
amount)

-- | Construct a constant singleton 'PValue' containing only the given positive quantity of the given currency.
pconstantPositiveSingleton ::
  ClosedTerm PCurrencySymbol ->
  ClosedTerm PTokenName ->
  ClosedTerm PInteger ->
  ClosedTerm (PValue 'Sorted 'Positive)
pconstantPositiveSingleton :: ClosedTerm PCurrencySymbol
-> ClosedTerm PTokenName
-> ClosedTerm PInteger
-> ClosedTerm (PValue 'Sorted 'Positive)
pconstantPositiveSingleton ClosedTerm PCurrencySymbol
symbol ClosedTerm PTokenName
token ClosedTerm PInteger
amount
  | forall (p :: PType).
(HasCallStack, PLift p) =>
ClosedTerm p -> PLifted p
plift ClosedTerm PInteger
amount forall a. Eq a => a -> a -> Bool
== PLifted PInteger
0 = forall a. Monoid a => a
mempty
  | forall (p :: PType).
(HasCallStack, PLift p) =>
ClosedTerm p -> PLifted p
plift ClosedTerm PInteger
amount forall a. Ord a => a -> a -> Bool
< PLifted PInteger
0 = forall a. HasCallStack => [Char] -> a
error [Char]
"Negative amount"
  | Bool
otherwise = forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast (forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PCurrencySymbol
symbol forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PTokenName
token forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# ClosedTerm PInteger
amount)

-- | Construct a singleton 'PValue' containing only the given quantity of the given currency.
psingleton ::
  Term
    s
    (PCurrencySymbol :--> PTokenName :--> PInteger :--> PValue 'Sorted 'NonZero)
psingleton :: forall (s :: S).
Term
  s
  (PCurrencySymbol
   :--> (PTokenName :--> (PInteger :--> PValue 'Sorted 'NonZero)))
psingleton = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
symbol Term s PTokenName
token Term s PInteger
amount ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (Term s PInteger
amount forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
0)
      forall a. Monoid a => a
mempty
      (forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast forall a b. (a -> b) -> a -> b
$ forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
symbol forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (k :--> (v :--> PMap 'Sorted k v))
AssocMap.psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PTokenName
token forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PInteger
amount)

{- | Construct a singleton 'PValue' containing only the given quantity of the
 given currency, taking data-encoded parameters.
-}
psingletonData ::
  Term
    s
    ( PAsData PCurrencySymbol
        :--> PAsData PTokenName
        :--> PAsData PInteger
        :--> PValue 'Sorted 'NonZero
    )
psingletonData :: forall (s :: S).
Term
  s
  (PAsData PCurrencySymbol
   :--> (PAsData PTokenName
         :--> (PAsData PInteger :--> PValue 'Sorted 'NonZero)))
psingletonData = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PAsData PCurrencySymbol)
symbol Term s (PAsData PTokenName)
token Term s (PAsData PInteger)
amount ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (Term s (PAsData PInteger)
amount forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== ClosedTerm (PAsData PInteger)
zeroData)
      forall a. Monoid a => a
mempty
      ( forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast
          ( forall (s :: S) (k :: PType) (v :: PType).
Term s (PAsData k :--> (PAsData v :--> PMap 'Sorted k v))
AssocMap.psingletonData
              # symbol
                #$ pdata
              $ AssocMap.psingletonData # token # amount
          )
      )

-- | Get the quantity of the given currency in the 'PValue'.
pvalueOf :: Term s (PValue anyKey anyAmount :--> PCurrencySymbol :--> PTokenName :--> PInteger)
pvalueOf :: forall (s :: S) (anyKey :: KeyGuarantees)
       (anyAmount :: AmountGuarantees).
Term
  s
  (PValue anyKey anyAmount
   :--> (PCurrencySymbol :--> (PTokenName :--> PInteger)))
pvalueOf = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue anyKey anyAmount)
value Term s PCurrencySymbol
symbol Term s PTokenName
token ->
    forall (k :: PType) (s :: S) (r :: PType) (v :: PType)
       (any :: KeyGuarantees).
PIsData k =>
Term
  s (k :--> (r :--> ((PAsData v :--> r) :--> (PMap any k v :--> r))))
AssocMap.pfoldAt
      # symbol
      # 0
      # plam (\m -> AssocMap.pfoldAt # token # 0 # plam pfromData # pfromData m)
      # pto value

-- | The 'PCurrencySymbol' of the Ada currency.
padaSymbol :: Term s PCurrencySymbol
padaSymbol :: ClosedTerm PCurrencySymbol
padaSymbol = forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant CurrencySymbol
Plutus.adaSymbol

-- | Data-encoded 'PCurrencySymbol' of the Ada currency.
padaSymbolData :: Term s (PAsData PCurrencySymbol)
padaSymbolData :: forall (s :: S). Term s (PAsData PCurrencySymbol)
padaSymbolData = forall (a :: PType) (s :: S).
PIsData a =>
Term s a -> Term s (PAsData a)
pdata ClosedTerm PCurrencySymbol
padaSymbol

-- | The 'PTokenName' of the Ada currency.
padaToken :: Term s PTokenName
padaToken :: ClosedTerm PTokenName
padaToken = forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant TokenName
Plutus.adaToken

-- | Data-encoded 'PTokenName' of the Ada currency.
padaTokenData :: Term s (PAsData PTokenName)
padaTokenData :: forall (s :: S). Term s (PAsData PTokenName)
padaTokenData = forall (a :: PType) (s :: S).
PIsData a =>
Term s a -> Term s (PAsData a)
pdata ClosedTerm PTokenName
padaToken

-- | Test if the value contains nothing but Ada
pisAdaOnlyValue :: Term s (PValue 'Sorted 'Positive :--> PBool)
pisAdaOnlyValue :: forall (s :: S). Term s (PValue 'Sorted 'Positive :--> PBool)
pisAdaOnlyValue = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue 'Sorted 'Positive)
value ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted 'Positive)
value) forall a b. (a -> b) -> a -> b
$ \case
      PInner
  (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) s
PBuiltinList
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
  s
PNil -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue
      PCons Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
xs -> forall (s :: S). Term s (PBool :--> (PBool :--> PBool))
pand' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (list a :--> PBool)
pnull forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
xs) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (s :: S) (a :: PType) (b :: PType).
Term s (PBuiltinPair a b :--> a)
pfstBuiltin forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== forall (s :: S). Term s (PAsData PCurrencySymbol)
padaSymbolData)

-- | Value without any non-Ada
padaOnlyValue :: Term s (PValue 'Sorted v :--> PValue 'Sorted v)
padaOnlyValue :: forall (s :: S) (v :: AmountGuarantees).
Term s (PValue 'Sorted v :--> PValue 'Sorted v)
padaOnlyValue = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue 'Sorted v)
value ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted v)
value) forall a b. (a -> b) -> a -> b
$ \case
      PInner
  (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) s
PBuiltinList
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
  s
PNil -> Term s (PValue 'Sorted v)
value
      PCons Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
_ ->
        forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
          # (pfstBuiltin # x #== padaSymbolData)
          # pcon (PValue $ pcon $ AssocMap.PMap $ List.psingleton # x)
          # pcon (PValue AssocMap.pempty)

-- | Value without any Ada
pnoAdaValue :: Term s (PValue 'Sorted v :--> PValue 'Sorted v)
pnoAdaValue :: forall (s :: S) (v :: AmountGuarantees).
Term s (PValue 'Sorted v :--> PValue 'Sorted v)
pnoAdaValue = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue 'Sorted v)
value ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted v)
value) forall a b. (a -> b) -> a -> b
$ \case
      PInner
  (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) s
PBuiltinList
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
  s
PNil -> Term s (PValue 'Sorted v)
value
      PCons Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
xs -> forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (s :: S) (a :: PType) (b :: PType).
Term s (PBuiltinPair a b :--> a)
pfstBuiltin forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== forall (s :: S). Term s (PAsData PCurrencySymbol)
padaSymbolData) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon (forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall a b. (a -> b) -> a -> b
$ forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall a b. (a -> b) -> a -> b
$ forall (keysort :: KeyGuarantees) (k :: PType) (v :: PType)
       (s :: S).
Term s (PBuiltinList (PBuiltinPair (PAsData k) (PAsData v)))
-> PMap keysort k v s
AssocMap.PMap Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
xs) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PValue 'Sorted v)
value

-- | The amount of Lovelace in value
plovelaceValueOf :: Term s (PValue 'Sorted v :--> PInteger)
plovelaceValueOf :: forall (s :: S) (v :: AmountGuarantees).
Term s (PValue 'Sorted v :--> PInteger)
plovelaceValueOf = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue 'Sorted v)
value ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted v)
value) forall a b. (a -> b) -> a -> b
$ \case
      PInner
  (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) s
PBuiltinList
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
  s
PNil -> Term s PInteger
0
      PCons Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol)
     (PAsData (PMap 'Sorted PTokenName PInteger)))
x Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol)
        (PAsData (PMap 'Sorted PTokenName PInteger))))
_ ->
        forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
          # (pfstBuiltin # x #== padaSymbolData)
          # pfromData (psndBuiltin #$ phead #$ pto $ pfromData $ psndBuiltin # x)
          # 0

{- | Combine two 'PValue's applying the given function to any pair of
 quantities with the same asset class. Note that the result is _not_
 'normalize'd and may contain zero quantities.
-}
punionWith ::
  Term
    s
    ( (PInteger :--> PInteger :--> PInteger)
        :--> PValue 'Sorted any0
        :--> PValue 'Sorted any1
        :--> PValue 'Sorted 'NoGuarantees
    )
punionWith :: forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWith = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PInteger :--> (PInteger :--> PInteger))
combine Term s (PValue 'Sorted any0)
x Term s (PValue 'Sorted any1)
y ->
    forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall a b. (a -> b) -> a -> b
$
      forall (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((v :--> (v :--> v))
   :--> (PMap 'Sorted k v
         :--> (PMap 'Sorted k v :--> PMap 'Sorted k v)))
AssocMap.punionWith
        # plam (\x y -> AssocMap.punionWith # combine # x # y)
        # pto x
        # pto y

{- | Combine two 'PValue's applying the given function to any pair of
 data-encoded quantities with the same asset class. Note that the result is
 _not_ 'normalize'd and may contain zero quantities.
-}
punionWithData ::
  Term
    s
    ( (PAsData PInteger :--> PAsData PInteger :--> PAsData PInteger)
        :--> PValue 'Sorted any0
        :--> PValue 'Sorted any1
        :--> PValue 'Sorted 'NoGuarantees
    )
punionWithData :: forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PAsData PInteger :--> (PAsData PInteger :--> PAsData PInteger))
   :--> (PValue 'Sorted any0
         :--> (PValue 'Sorted any1 :--> PValue 'Sorted 'NoGuarantees)))
punionWithData = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term
  s (PAsData PInteger :--> (PAsData PInteger :--> PAsData PInteger))
combine Term s (PValue 'Sorted any0)
x Term s (PValue 'Sorted any1)
y ->
    forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall a b. (a -> b) -> a -> b
$
      forall (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((v :--> (v :--> v))
   :--> (PMap 'Sorted k v
         :--> (PMap 'Sorted k v :--> PMap 'Sorted k v)))
AssocMap.punionWith
        # plam (\x y -> AssocMap.punionWithData # combine # x # y)
        # pto x
        # pto y

-- | Normalize the argument to contain no zero quantity nor empty token map.
pnormalize :: Term s (PValue 'Sorted any :--> PValue 'Sorted 'NonZero)
pnormalize :: forall (s :: S) (any :: AmountGuarantees).
Term s (PValue 'Sorted any :--> PValue 'Sorted 'NonZero)
pnormalize = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue 'Sorted any)
value ->
    forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall a b. (a -> b) -> a -> b
$
      forall (a :: PType) (b :: PType) (s :: S) (g :: KeyGuarantees)
       (k :: PType).
(PIsData a, PIsData b) =>
Term s ((a :--> PMaybe b) :--> (PMap g k a :--> PMap g k b))
AssocMap.pmapMaybe forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall {s :: S} {g :: KeyGuarantees} {k :: PType}.
Term s (PMap g k PInteger) -> Term s (PMaybe (PMap g k PInteger))
normalizeTokenMap forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted any)
value
  where
    normalizeTokenMap :: Term s (PMap g k PInteger) -> Term s (PMaybe (PMap g k PInteger))
normalizeTokenMap Term s (PMap g k PInteger)
tokenMap =
      forall (s :: S) (a :: PType) (b :: PType).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (forall (s :: S) (a :: PType) (b :: PType) (g :: KeyGuarantees)
       (k :: PType).
Term
  s
  ((PAsData a :--> PMaybe (PAsData b))
   :--> (PMap g k a :--> PMap g k b))
AssocMap.pmapMaybeData forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall {s :: S}.
Term s (PAsData PInteger) -> Term s (PMaybe (PAsData PInteger))
nonZero forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap g k PInteger)
tokenMap) forall a b. (a -> b) -> a -> b
$ \Term s (PMap g k PInteger)
normalMap ->
        forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
          (forall (s :: S) (any :: KeyGuarantees) (k :: PType) (v :: PType).
Term s (PMap any k v :--> PBool)
AssocMap.pnull forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap g k PInteger)
normalMap)
          (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (a :: PType) (s :: S). PMaybe a s
PNothing)
          (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall a b. (a -> b) -> a -> b
$ forall (a :: PType) (s :: S). Term s a -> PMaybe a s
PJust Term s (PMap g k PInteger)
normalMap)
    nonZero :: Term s (PAsData PInteger) -> Term s (PMaybe (PAsData PInteger))
nonZero Term s (PAsData PInteger)
intData =
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (Term s (PAsData PInteger)
intData forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== ClosedTerm (PAsData PInteger)
zeroData) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (a :: PType) (s :: S). PMaybe a s
PNothing) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall a b. (a -> b) -> a -> b
$ forall (a :: PType) (s :: S). Term s a -> PMaybe a s
PJust Term s (PAsData PInteger)
intData)

-- | Assert the value is properly sorted and normalized.
passertSorted :: Term s (PValue anyKey anyAmount :--> PValue 'Sorted 'NonZero)
passertSorted :: forall (s :: S) (anyKey :: KeyGuarantees)
       (anyAmount :: AmountGuarantees).
Term s (PValue anyKey anyAmount :--> PValue 'Sorted 'NonZero)
passertSorted = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue anyKey anyAmount)
value ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      ( forall (v :: PType) (s :: S) (any :: KeyGuarantees) (k :: PType).
PIsData v =>
Term s ((v :--> PBool) :--> (PMap any k v :--> PBool))
AssocMap.pany
          # plam
            ( \submap ->
                AssocMap.pnull
                  # (AssocMap.passertSorted # submap)
                    #|| AssocMap.pany
                  # plam (#== 0)
                  # submap
            )
          # pto value
      )
      (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"Abnormal Value")
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue
      forall a b. (a -> b) -> a -> b
$ forall (k :: PType) (v :: PType) (any :: KeyGuarantees) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term s (PMap any k v :--> PMap 'Sorted k v)
AssocMap.passertSorted forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce
      forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue anyKey anyAmount)
value

-- | Assert all amounts in the value are positive.
passertPositive :: forall kg ag s. Term s (PValue kg ag :--> PValue kg 'Positive)
passertPositive :: forall (kg :: KeyGuarantees) (ag :: AmountGuarantees) (s :: S).
Term s (PValue kg ag :--> PValue kg 'Positive)
passertPositive = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue kg ag)
value ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      ( forall (v :: PType) (s :: S) (any :: KeyGuarantees) (k :: PType).
PIsData v =>
Term s ((v :--> PBool) :--> (PMap any k v :--> PBool))
AssocMap.pall
          # plam (\submap -> AssocMap.pall # plam (0 #<) # submap)
          # pto value
      )
      (forall (s :: S) (a :: PType). Term s (PInner a) -> Term s a
punsafeDowncast forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue kg ag)
value)
      (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"Negative amount in Value")

passertNonZero :: forall kg ag. ClosedTerm (PValue kg ag :--> PValue kg 'NonZero)
passertNonZero :: forall (kg :: KeyGuarantees) (ag :: AmountGuarantees).
ClosedTerm (PValue kg ag :--> PValue kg 'NonZero)
passertNonZero = forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PValue kg ag)
val ->
  forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (forall (k :: KeyGuarantees).
ClosedTerm
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
   :--> PBool)
outer forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall a b. (a -> b) -> a -> b
$ Term s (PValue kg ag)
val) (forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce Term s (PValue kg ag)
val) (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"Zero amount in Value")
  where
    outer :: ClosedTerm (PBuiltinList (PBuiltinPair (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger))) :--> PBool)
    outer :: forall (k :: KeyGuarantees).
ClosedTerm
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
   :--> PBool)
outer = forall (s :: S) (a :: PType) (b :: PType).
Term s (((a :--> b) :--> (a :--> b)) :--> (a :--> b))
pfix forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
   :--> PBool)
self Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger))))
m ->
      forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger))))
m forall a b. (a -> b) -> a -> b
$ \case
        PCons Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
x Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger))))
xs -> ClosedTerm
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
   :--> PBool)
inner forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData forall a b. (a -> b) -> a -> b
$ forall (s :: S) (a :: PType) (b :: PType).
Term s (PBuiltinPair a b :--> b)
psndBuiltin forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinPair
     (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
x) forall (s :: S). Term s PBool -> Term s PBool -> Term s PBool
#&& Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
   :--> PBool)
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinList
     (PBuiltinPair
        (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger))))
xs
        PBuiltinList
  (PBuiltinPair
     (PAsData PCurrencySymbol) (PAsData (PMap k PTokenName PInteger)))
  s
PNil -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue
    inner :: ClosedTerm (PBuiltinList (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)) :--> PBool)
    inner :: ClosedTerm
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
   :--> PBool)
inner = forall (s :: S) (a :: PType) (b :: PType).
Term s (((a :--> b) :--> (a :--> b)) :--> (a :--> b))
pfix forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
   :--> PBool)
self Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)))
m ->
      forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)))
m forall a b. (a -> b) -> a -> b
$ \case
        PCons Term s (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
x Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)))
xs -> forall (s :: S). Term s (PBool :--> PBool)
pnot forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (s :: S) (a :: PType) (b :: PType).
Term s (PBuiltinPair a b :--> b)
psndBuiltin forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
x forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== forall (p :: PType) h (s :: S).
(ToData h, (PLifted p :: Type) ~ (h :: Type),
 (PConstanted h :: PType) ~ (p :: PType)) =>
h -> Term s (PAsData p)
pconstantData Integer
0) forall (s :: S). Term s PBool -> Term s PBool -> Term s PBool
#&& Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger))
   :--> PBool)
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PBuiltinList
     (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)))
xs
        PBuiltinList
  (PBuiltinPair (PAsData PTokenName) (PAsData PInteger)) s
PNil -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue

-- | Forget the knowledge of value's positivity.
pforgetPositive :: Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive :: forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term s (PValue k 'Positive) -> Term s (PValue k a)
pforgetPositive = forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce

-- | Forget the knowledge of all value's guarantees.
pforgetSorted :: Term s (PValue 'Sorted a) -> Term s (PValue k a)
pforgetSorted :: forall (s :: S) (a :: AmountGuarantees) (k :: KeyGuarantees).
Term s (PValue 'Sorted a) -> Term s (PValue k a)
pforgetSorted = forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce

zeroData :: ClosedTerm (PAsData PInteger)
zeroData :: ClosedTerm (PAsData PInteger)
zeroData = forall (a :: PType) (s :: S).
PIsData a =>
Term s a -> Term s (PAsData a)
pdata Term s PInteger
0

-- | Applies a function to every amount in the map.
pmapAmounts :: Term s ((PInteger :--> PInteger) :--> PValue k a :--> PValue k 'NoGuarantees)
pmapAmounts :: forall (s :: S) (k :: KeyGuarantees) (a :: AmountGuarantees).
Term
  s
  ((PInteger :--> PInteger)
   :--> (PValue k a :--> PValue k 'NoGuarantees))
pmapAmounts = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$
    \Term s (PInteger :--> PInteger)
f Term s (PValue k a)
v -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall a b. (a -> b) -> a -> b
$ forall (keys :: KeyGuarantees) (amounts :: AmountGuarantees)
       (s :: S).
Term s (PMap keys PCurrencySymbol (PMap keys PTokenName PInteger))
-> PValue keys amounts s
PValue forall a b. (a -> b) -> a -> b
$ forall (a :: PType) (b :: PType) (s :: S) (g :: KeyGuarantees)
       (k :: PType).
(PIsData a, PIsData b) =>
Term s ((a :--> b) :--> (PMap g k a :--> PMap g k b))
AssocMap.pmap forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam (forall (a :: PType) (b :: PType) (s :: S) (g :: KeyGuarantees)
       (k :: PType).
(PIsData a, PIsData b) =>
Term s ((a :--> b) :--> (PMap g k a :--> PMap g k b))
AssocMap.pmap forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PInteger :--> PInteger)
f #) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue k a)
v

{- | Given an amount comparison function, check whether a binary relation holds over
2 sorted 'PValue's.
-}
pcheckBinRel :: Term s ((PInteger :--> PInteger :--> PBool) :--> PValue 'Sorted any0 :--> PValue 'Sorted any1 :--> PBool)
pcheckBinRel :: forall (s :: S) (any0 :: AmountGuarantees)
       (any1 :: AmountGuarantees).
Term
  s
  ((PInteger :--> (PInteger :--> PBool))
   :--> (PValue 'Sorted any0 :--> (PValue 'Sorted any1 :--> PBool)))
pcheckBinRel = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  forall a (b :: PType) (s :: S) (c :: PType).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
plam forall a b. (a -> b) -> a -> b
$ \Term s (PInteger :--> (PInteger :--> PBool))
f ->
    forall (s :: S) (a :: PType) (b :: PType) (c :: PType).
Term s (PInner a :--> (PInner b :--> c))
-> Term s (a :--> (b :--> c))
subReduction2 forall a b. (a -> b) -> a -> b
$
      forall (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((v :--> (v :--> PBool))
   :--> (v
         :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))
AssocMap.pcheckBinRel forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (forall (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((v :--> (v :--> PBool))
   :--> (v
         :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))
AssocMap.pcheckBinRel forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PInteger :--> (PInteger :--> PBool))
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PInteger
0) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (s :: S) (k :: PType) (v :: PType).
Term s (PMap 'Sorted k v)
AssocMap.pempty
  where
    subReduction2 :: Term s (PInner a :--> PInner b :--> c) -> Term s (a :--> b :--> c)
    subReduction2 :: forall (s :: S) (a :: PType) (b :: PType) (c :: PType).
Term s (PInner a :--> (PInner b :--> c))
-> Term s (a :--> (b :--> c))
subReduction2 = forall (s :: S) (a :: PType) (b :: PType). Term s a -> Term s b
punsafeCoerce