{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE RankNTypes #-}
{-# OPTIONS_GHC -Wno-deprecations #-}

-- Mirrors the equivalent V4 module in plutus-ledger-api
module Plutarch.LedgerApi.V4.Ratio (
  PRationalData (..),
  prationalFromData,
) where

import Data.Kind (Type)
import GHC.Generics (Generic)
import Generics.SOP qualified as SOP
import Plutarch.Internal.Lift (
  LiftError (CouldNotDecodeData),
  punsafeCoercePLifted,
 )
import Plutarch.Prelude
import Plutarch.Unsafe (punsafeCoerce)
import PlutusLedgerApi.V4 qualified as Plutus

-- | @since 3.8.0
data PRationalData (s :: S) = PRationalData
  { forall (s :: S). PRationalData s -> Term s (PAsData PInteger)
prationalData'numerator :: Term s (PAsData PInteger)
  , forall (s :: S). PRationalData s -> Term s (PAsData PPositive)
prationalData'denominator :: Term s (PAsData PPositive)
  }
  deriving stock
    ( -- | @since 3.8.0
      (forall x. PRationalData s -> Rep (PRationalData s) x)
-> (forall x. Rep (PRationalData s) x -> PRationalData s)
-> Generic (PRationalData s)
forall x. Rep (PRationalData s) x -> PRationalData s
forall x. PRationalData s -> Rep (PRationalData s) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (PRationalData s) x -> PRationalData s
forall (s :: S) x. PRationalData s -> Rep (PRationalData s) x
$cfrom :: forall (s :: S) x. PRationalData s -> Rep (PRationalData s) x
from :: forall x. PRationalData s -> Rep (PRationalData s) x
$cto :: forall (s :: S) x. Rep (PRationalData s) x -> PRationalData s
to :: forall x. Rep (PRationalData s) x -> PRationalData s
Generic
    )
  deriving anyclass
    ( -- | @since 3.8.0
      All SListI (Code (PRationalData s))
All SListI (Code (PRationalData s)) =>
(PRationalData s -> Rep (PRationalData s))
-> (Rep (PRationalData s) -> PRationalData s)
-> Generic (PRationalData s)
Rep (PRationalData s) -> PRationalData s
PRationalData s -> Rep (PRationalData s)
forall a.
All SListI (Code a) =>
(a -> Rep a) -> (Rep a -> a) -> Generic a
forall (s :: S). All SListI (Code (PRationalData s))
forall (s :: S). Rep (PRationalData s) -> PRationalData s
forall (s :: S). PRationalData s -> Rep (PRationalData s)
$cfrom :: forall (s :: S). PRationalData s -> Rep (PRationalData s)
from :: PRationalData s -> Rep (PRationalData s)
$cto :: forall (s :: S). Rep (PRationalData s) -> PRationalData s
to :: Rep (PRationalData s) -> PRationalData s
SOP.Generic
    , -- | @since 3.8.0
      (forall (s :: S).
 Term s (PAsData PRationalData) -> Term s PRationalData)
-> (forall (s :: S). Term s PRationalData -> Term s PData)
-> PIsData PRationalData
forall (s :: S).
Term s (PAsData PRationalData) -> Term s PRationalData
forall (s :: S). Term s PRationalData -> Term s PData
forall (a :: S -> Type).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
$cpfromDataImpl :: forall (s :: S).
Term s (PAsData PRationalData) -> Term s PRationalData
pfromDataImpl :: forall (s :: S).
Term s (PAsData PRationalData) -> Term s PRationalData
$cpdataImpl :: forall (s :: S). Term s PRationalData -> Term s PData
pdataImpl :: forall (s :: S). Term s PRationalData -> Term s PData
PIsData
    , -- | @since 3.8.0
      (forall (s :: S).
 Term s PRationalData -> Term s PRationalData -> Term s PBool)
-> PEq PRationalData
forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
forall (t :: S -> Type).
(forall (s :: S). Term s t -> Term s t -> Term s PBool) -> PEq t
$c#== :: forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
#== :: forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
PEq
    , -- | @since 3.8.0
      (forall (s :: S). Bool -> Term s PRationalData -> Term s PString)
-> PShow PRationalData
forall (s :: S). Bool -> Term s PRationalData -> Term s PString
forall (t :: S -> Type).
(forall (s :: S). Bool -> Term s t -> Term s PString) -> PShow t
$cpshow' :: forall (s :: S). Bool -> Term s PRationalData -> Term s PString
pshow' :: forall (s :: S). Bool -> Term s PRationalData -> Term s PString
PShow
    )
  deriving
    ( -- | @since 3.8.0
      (forall (s :: S). PRationalData s -> Term s (PInner PRationalData))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner PRationalData)
    -> (PRationalData s -> Term s b) -> Term s b)
-> PlutusType PRationalData
forall (s :: S). PRationalData s -> Term s (PInner PRationalData)
forall (s :: S) (b :: S -> Type).
Term s (PInner PRationalData)
-> (PRationalData s -> Term s b) -> Term s b
forall (a :: S -> Type).
(forall (s :: S). a s -> Term s (PInner a))
-> (forall (s :: S) (b :: S -> Type).
    Term s (PInner a) -> (a s -> Term s b) -> Term s b)
-> PlutusType a
$cpcon' :: forall (s :: S). PRationalData s -> Term s (PInner PRationalData)
pcon' :: forall (s :: S). PRationalData s -> Term s (PInner PRationalData)
$cpmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PRationalData)
-> (PRationalData s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: S -> Type).
Term s (PInner PRationalData)
-> (PRationalData s -> Term s b) -> Term s b
PlutusType
    )
    via (DeriveAsDataRec PRationalData)

-- | @since 3.8.0
instance POrd PRationalData where
  #<= :: forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
(#<=) = (forall (s' :: S).
 Term s' PInteger -> Term s' PInteger -> Term s' PBool)
-> Term s PRationalData -> Term s PRationalData -> Term s PBool
forall (s :: S).
(forall (s' :: S).
 Term s' PInteger -> Term s' PInteger -> Term s' PBool)
-> Term s PRationalData -> Term s PRationalData -> Term s PBool
liftCompareOp Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
POrd t =>
Term s t -> Term s t -> Term s PBool
(#<=)
  #< :: forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
(#<) = (forall (s' :: S).
 Term s' PInteger -> Term s' PInteger -> Term s' PBool)
-> Term s PRationalData -> Term s PRationalData -> Term s PBool
forall (s :: S).
(forall (s' :: S).
 Term s' PInteger -> Term s' PInteger -> Term s' PBool)
-> Term s PRationalData -> Term s PRationalData -> Term s PBool
liftCompareOp Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
POrd t =>
Term s t -> Term s t -> Term s PBool
(#<)

-- | @since 3.8.0
instance PLiftable PRationalData where
  type AsHaskell PRationalData = Plutus.Rational
  type PlutusRepr PRationalData = Plutus.Data
  haskToRepr :: AsHaskell PRationalData -> PlutusRepr PRationalData
haskToRepr = AsHaskell PRationalData -> PlutusRepr PRationalData
Rational -> Data
forall a. ToData a => a -> Data
Plutus.toData
  reprToHask :: PlutusRepr PRationalData
-> Either LiftError (AsHaskell PRationalData)
reprToHask = Either LiftError Rational
-> (Rational -> Either LiftError Rational)
-> Maybe Rational
-> Either LiftError Rational
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (LiftError -> Either LiftError Rational
forall a b. a -> Either a b
Left LiftError
CouldNotDecodeData) Rational -> Either LiftError Rational
forall a b. b -> Either a b
Right (Maybe Rational -> Either LiftError Rational)
-> (Data -> Maybe Rational) -> Data -> Either LiftError Rational
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Data -> Maybe Rational
forall a. FromData a => Data -> Maybe a
Plutus.fromData
  plutToRepr :: (forall (s :: S). PLifted s PRationalData)
-> Either LiftError (PlutusRepr PRationalData)
plutToRepr forall (s :: S). PLifted s PRationalData
t = ([Data] -> PlutusRepr PRationalData)
-> Either LiftError [Data]
-> Either LiftError (PlutusRepr PRationalData)
forall a b. (a -> b) -> Either LiftError a -> Either LiftError b
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap [Data] -> PlutusRepr PRationalData
[Data] -> Data
Plutus.List (Either LiftError [Data]
 -> Either LiftError (PlutusRepr PRationalData))
-> Either LiftError [Data]
-> Either LiftError (PlutusRepr PRationalData)
forall a b. (a -> b) -> a -> b
$ forall (a :: S -> Type).
KnownBuiltinType
  (Term DeBruijn DefaultUni DefaultFun ()) (PlutusRepr a) =>
(forall (s :: S). PLifted s a) -> Either LiftError (PlutusRepr a)
plutToReprUni @(PBuiltinList PData) ((forall (s :: S). PLifted s (PBuiltinList PData))
 -> Either LiftError (PlutusRepr (PBuiltinList PData)))
-> (forall (s :: S). PLifted s (PBuiltinList PData))
-> Either LiftError (PlutusRepr (PBuiltinList PData))
forall a b. (a -> b) -> a -> b
$ PLifted s PRationalData -> PLifted s (PBuiltinList PData)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
PLifted s a -> PLifted s b
punsafeCoercePLifted PLifted s PRationalData
forall (s :: S). PLifted s PRationalData
t
  reprToPlut :: forall (s :: S).
PlutusRepr PRationalData -> PLifted s PRationalData
reprToPlut = \case
    Plutus.List [Data]
ell -> PLifted s (PBuiltinList PData) -> PLifted s PRationalData
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
PLifted s a -> PLifted s b
punsafeCoercePLifted (forall (a :: S -> Type) (s :: S).
Includes DefaultUni (PlutusRepr a) =>
PlutusRepr a -> PLifted s a
reprToPlutUni @(PBuiltinList PData) [Data]
PlutusRepr (PBuiltinList PData)
ell)
    PlutusRepr PRationalData
_ -> [Char] -> PLifted s PRationalData
forall a. HasCallStack => [Char] -> a
error [Char]
"Impossible: PRationalData is list-represented."

{- | Checks that we have a @Data@-encoded list with exactly two elements, that
the first element is a 'PInteger', that the second is a 'PPositive', and that
the fraction they form is in reduced form.

@since 3.8.0
-}
instance PValidateData PRationalData where
  pwithValidated :: forall (s :: S).
Term s PData -> forall (r :: S -> Type). Term s r -> Term s r
pwithValidated Term s PData
d Term s r
r = Term s (PBuiltinList PData)
-> (Term s (PBuiltinList PData) -> Term s r) -> Term s r
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s (PData :--> PBuiltinList PData)
forall (s :: S). Term s (PData :--> PBuiltinList PData)
pasList Term s (PData :--> PBuiltinList PData)
-> Term s PData -> Term s (PBuiltinList PData)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PData
d) ((Term s (PBuiltinList PData) -> Term s r) -> Term s r)
-> (Term s (PBuiltinList PData) -> Term s r) -> Term s r
forall a b. (a -> b) -> a -> b
$ \Term s (PBuiltinList PData)
ell -> Term s (PBuiltinList PData)
-> (Term s PData -> Term s (PBuiltinList PData) -> Term s r)
-> Term s r
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> (Term s a -> Term s (PBuiltinList a) -> Term s b) -> Term s b
pheadTailBuiltin Term s (PBuiltinList PData)
ell ((Term s PData -> Term s (PBuiltinList PData) -> Term s r)
 -> Term s r)
-> (Term s PData -> Term s (PBuiltinList PData) -> Term s r)
-> Term s r
forall a b. (a -> b) -> a -> b
$
    \Term s PData
h1 Term s (PBuiltinList PData)
t1 -> Term s (PBuiltinList PData)
-> (Term s PData -> Term s (PBuiltinList PData) -> Term s r)
-> Term s r
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> (Term s a -> Term s (PBuiltinList a) -> Term s b) -> Term s b
pheadTailBuiltin Term s (PBuiltinList PData)
t1 ((Term s PData -> Term s (PBuiltinList PData) -> Term s r)
 -> Term s r)
-> (Term s PData -> Term s (PBuiltinList PData) -> Term s r)
-> Term s r
forall a b. (a -> b) -> a -> b
$ \Term s PData
h2 Term s (PBuiltinList PData)
t2 -> Term s (PBuiltinList PData)
-> (PBuiltinList PData s -> Term s r) -> Term s r
forall (a :: S -> Type) (s :: S) (b :: S -> Type).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PBuiltinList PData)
t2 ((PBuiltinList PData s -> Term s r) -> Term s r)
-> (PBuiltinList PData s -> Term s r) -> Term s r
forall a b. (a -> b) -> a -> b
$ \case
      PBuiltinList PData s
PNil ->
        let num :: Term s (PAsData PInteger)
num = forall (a :: S -> Type) (s :: S).
(PIsData a, PValidateData a) =>
Term s PData -> Term s (PAsData a)
pparseData @PInteger Term s PData
h1
            den :: Term s (PAsData PPositive)
den = forall (a :: S -> Type) (s :: S).
(PIsData a, PValidateData a) =>
Term s PData -> Term s (PAsData a)
pparseData @PPositive Term s PData
h2
         in Term s PBool -> Term s r -> Term s r -> Term s r
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
              ((Term s (PInteger :--> (PInteger :--> PInteger))
forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PAsData PInteger) -> Term s PInteger
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s (PAsData PInteger)
num Term s (PInteger :--> PInteger)
-> Term s PInteger -> Term s PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PNatural -> Term s (PInner PNatural)
forall (a :: S -> Type) (s :: S). Term s a -> Term s (PInner a)
pto (Term s PPositive -> Term s (PInner PPositive)
forall (a :: S -> Type) (s :: S). Term s a -> Term s (PInner a)
pto (Term s (PAsData PPositive) -> Term s PPositive
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s (PAsData PPositive)
den))) Term s PInteger -> Term s PInteger -> Term s PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== AsHaskell PInteger -> Term s PInteger
forall (a :: S -> Type) (s :: S).
PLiftable a =>
AsHaskell a -> Term s a
pconstant Integer
AsHaskell PInteger
1)
              Term s r
r
              Term s r
forall (s :: S) (a :: S -> Type). Term s a
perror
      PCons Term s PData
_ Term s (PBuiltinList PData)
_ -> Term s r
forall (s :: S) (a :: S -> Type). Term s a
perror

-- | @since 3.8.0
instance PTryFrom PData (PAsData PRationalData) where
  ptryFrom' :: forall (s :: S) (r :: S -> Type).
Term s PData
-> ((Term s (PAsData PRationalData),
     Reduce (PTryFromExcess PData (PAsData PRationalData) s))
    -> Term s r)
-> Term s r
ptryFrom' Term s PData
opq = TermCont
  s
  (Term s (PAsData PRationalData),
   Reduce (PTryFromExcess PData (PAsData PRationalData) s))
-> ((Term s (PAsData PRationalData),
     Reduce (PTryFromExcess PData (PAsData PRationalData) s))
    -> Term s r)
-> Term s r
forall (r :: S -> Type) (s :: S) a.
TermCont s a -> (a -> Term s r) -> Term s r
runTermCont (TermCont
   s
   (Term s (PAsData PRationalData),
    Reduce (PTryFromExcess PData (PAsData PRationalData) s))
 -> ((Term s (PAsData PRationalData),
      Reduce (PTryFromExcess PData (PAsData PRationalData) s))
     -> Term s r)
 -> Term s r)
-> TermCont
     s
     (Term s (PAsData PRationalData),
      Reduce (PTryFromExcess PData (PAsData PRationalData) s))
-> ((Term s (PAsData PRationalData),
     Reduce (PTryFromExcess PData (PAsData PRationalData) s))
    -> Term s r)
-> Term s r
forall a b. (a -> b) -> a -> b
$ do
    Term s (PBuiltinList PData)
ell <- Term s (PBuiltinList PData)
-> TermCont s (Term s (PBuiltinList PData))
forall {r :: S -> Type} (s :: S) (a :: S -> Type).
Term s a -> TermCont s (Term s a)
pletC (Term s (PBuiltinList PData)
 -> TermCont s (Term s (PBuiltinList PData)))
-> Term s (PBuiltinList PData)
-> TermCont s (Term s (PBuiltinList PData))
forall a b. (a -> b) -> a -> b
$ Term s (PData :--> PBuiltinList PData)
forall (s :: S). Term s (PData :--> PBuiltinList PData)
pasList Term s (PData :--> PBuiltinList PData)
-> Term s PData -> Term s (PBuiltinList PData)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PData
opq
    (Term s PData
h1, Term s (PBuiltinList PData)
t1) <- Term s (PBuiltinList PData)
-> TermCont s (Term s PData, Term s (PBuiltinList PData))
forall {r :: S -> Type} (a :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> TermCont s (Term s a, Term s (PBuiltinList a))
pheadTailC Term s (PBuiltinList PData)
ell
    (Term s PData
h2, Term s (PBuiltinList PData)
t2) <- Term s (PBuiltinList PData)
-> TermCont s (Term s PData, Term s (PBuiltinList PData))
forall {r :: S -> Type} (a :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> TermCont s (Term s a, Term s (PBuiltinList a))
pheadTailC Term s (PBuiltinList PData)
t1
    Term s (PAsData PInteger)
num <- Term s (PAsData PInteger) -> TermCont s (Term s (PAsData PInteger))
forall {r :: S -> Type} (s :: S) (a :: S -> Type).
Term s a -> TermCont s (Term s a)
pletC (Term s (PAsData PInteger)
 -> TermCont s (Term s (PAsData PInteger)))
-> Term s (PAsData PInteger)
-> TermCont s (Term s (PAsData PInteger))
forall a b. (a -> b) -> a -> b
$ forall (b :: S -> Type) (a :: S -> Type) (s :: S) (r :: S -> Type).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PInteger) Term s PData
h1 (Term s (PAsData PInteger), Term s PInteger)
-> Term s (PAsData PInteger)
(Term s (PAsData PInteger),
 Reduce (PTryFromExcess PData (PAsData PInteger) s))
-> Term s (PAsData PInteger)
forall a b. (a, b) -> a
fst
    Term s (PAsData PPositive)
den <- Term s (PAsData PPositive)
-> TermCont s (Term s (PAsData PPositive))
forall {r :: S -> Type} (s :: S) (a :: S -> Type).
Term s a -> TermCont s (Term s a)
pletC (Term s (PAsData PPositive)
 -> TermCont s (Term s (PAsData PPositive)))
-> Term s (PAsData PPositive)
-> TermCont s (Term s (PAsData PPositive))
forall a b. (a -> b) -> a -> b
$ forall (b :: S -> Type) (a :: S -> Type) (s :: S) (r :: S -> Type).
PTryFrom a b =>
Term s a
-> ((Term s b, Reduce (PTryFromExcess a b s)) -> Term s r)
-> Term s r
ptryFrom @(PAsData PPositive) Term s PData
h2 (Term s (PAsData PPositive), ()) -> Term s (PAsData PPositive)
(Term s (PAsData PPositive),
 Reduce (PTryFromExcess PData (PAsData PPositive) s))
-> Term s (PAsData PPositive)
forall a b. (a, b) -> a
fst
    Term s PString -> Term s PBool -> TermCont s ()
forall {r :: S -> Type} (s :: S).
Term s PString -> Term s PBool -> TermCont s ()
pguardC Term s PString
"ptryFrom(PRationalData): too many fields" (Term s PBool -> TermCont s ()) -> Term s PBool -> TermCont s ()
forall a b. (a -> b) -> a -> b
$ Term s (PBuiltinList PData)
-> (PBuiltinList PData s -> Term s PBool) -> Term s PBool
forall (a :: S -> Type) (s :: S) (b :: S -> Type).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PBuiltinList PData)
t2 ((PBuiltinList PData s -> Term s PBool) -> Term s PBool)
-> (PBuiltinList PData s -> Term s PBool) -> Term s PBool
forall a b. (a -> b) -> a -> b
$ \case PBuiltinList PData s
PNil -> PBool s -> Term s PBool
forall (a :: S -> Type) (s :: S). PlutusType a => a s -> Term s a
pcon PBool s
forall (s :: S). PBool s
PTrue; PCons Term s PData
_ Term s (PBuiltinList PData)
_ -> PBool s -> Term s PBool
forall (a :: S -> Type) (s :: S). PlutusType a => a s -> Term s a
pcon PBool s
forall (s :: S). PBool s
PFalse
    Term s PString -> Term s PBool -> TermCont s ()
forall {r :: S -> Type} (s :: S).
Term s PString -> Term s PBool -> TermCont s ()
pguardC Term s PString
"ptryFrom(PRationalData): un-reduced fraction" (Term s PBool -> TermCont s ()) -> Term s PBool -> TermCont s ()
forall a b. (a -> b) -> a -> b
$ (Term s (PInteger :--> (PInteger :--> PInteger))
forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PAsData PInteger) -> Term s PInteger
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s (PAsData PInteger)
num Term s (PInteger :--> PInteger)
-> Term s PInteger -> Term s PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PNatural -> Term s (PInner PNatural)
forall (a :: S -> Type) (s :: S). Term s a -> Term s (PInner a)
pto (Term s PPositive -> Term s (PInner PPositive)
forall (a :: S -> Type) (s :: S). Term s a -> Term s (PInner a)
pto (Term s (PAsData PPositive) -> Term s PPositive
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s (PAsData PPositive)
den))) Term s PInteger -> Term s PInteger -> Term s PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== AsHaskell PInteger -> Term s PInteger
forall (a :: S -> Type) (s :: S).
PLiftable a =>
AsHaskell a -> Term s a
pconstant Integer
AsHaskell PInteger
1
    (Term s (PAsData PRationalData), ())
-> TermCont s (Term s (PAsData PRationalData), ())
forall a. a -> TermCont s a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s PData -> Term s (PAsData PRationalData)
forall (b :: S -> Type) (a :: S -> Type) (s :: S).
Term s a -> Term s b
punsafeCoerce Term s PData
opq, ())

-- | @since 3.8.0
prationalFromData :: forall (s :: S). Term s (PRationalData :--> PRational)
prationalFromData :: forall (s :: S). Term s (PRationalData :--> PRational)
prationalFromData = (forall (s :: S). Term s (PRationalData :--> PRational))
-> Term s (PRationalData :--> PRational)
forall (a :: S -> Type) (s :: S).
HasCallStack =>
(forall (s' :: S). Term s' a) -> Term s a
phoistAcyclic ((forall (s :: S). Term s (PRationalData :--> PRational))
 -> Term s (PRationalData :--> PRational))
-> (forall (s :: S). Term s (PRationalData :--> PRational))
-> Term s (PRationalData :--> PRational)
forall a b. (a -> b) -> a -> b
$
  (Term s' PRationalData -> Term s' PRational)
-> Term s' (PRationalData :--> PRational)
forall a (b :: S -> Type) (s :: S) (c :: S -> Type).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
forall (c :: S -> Type).
HasCallStack =>
(Term s' c -> Term s' PRational) -> Term s' (c :--> PRational)
plam ((Term s' PRationalData -> Term s' PRational)
 -> Term s' (PRationalData :--> PRational))
-> (Term s' PRationalData -> Term s' PRational)
-> Term s' (PRationalData :--> PRational)
forall a b. (a -> b) -> a -> b
$ \Term s' PRationalData
x -> TermCont s' (Term s' PRational) -> Term s' PRational
forall (a :: S -> Type) (s :: S). TermCont s (Term s a) -> Term s a
unTermCont (TermCont s' (Term s' PRational) -> Term s' PRational)
-> TermCont s' (Term s' PRational) -> Term s' PRational
forall a b. (a -> b) -> a -> b
$ do
    PRationalData Term s' (PAsData PInteger)
n Term s' (PAsData PPositive)
d <- Term s' PRationalData -> TermCont s' (PRationalData s')
forall {r :: S -> Type} (a :: S -> Type) (s :: S).
PlutusType a =>
Term s a -> TermCont s (a s)
pmatchC Term s' PRationalData
x
    Term s' PRational -> TermCont s' (Term s' PRational)
forall a. a -> TermCont s' a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s' PRational -> TermCont s' (Term s' PRational))
-> (PRational s' -> Term s' PRational)
-> PRational s'
-> TermCont s' (Term s' PRational)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PRational s' -> Term s' PRational
forall (a :: S -> Type) (s :: S). PlutusType a => a s -> Term s a
pcon (PRational s' -> TermCont s' (Term s' PRational))
-> PRational s' -> TermCont s' (Term s' PRational)
forall a b. (a -> b) -> a -> b
$ Term s' PInteger -> Term s' PPositive -> PRational s'
forall (s :: S). Term s PInteger -> Term s PPositive -> PRational s
PRational (Term s' (PAsData PInteger) -> Term s' PInteger
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PInteger)
n) (Term s' (PAsData PPositive) -> Term s' PPositive
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PPositive)
d)

-- Helpers

pheadTailC ::
  forall (a :: S -> Type) (s :: S).
  Term s (PBuiltinList a) ->
  TermCont s (Term s a, Term s (PBuiltinList a))
pheadTailC :: forall {r :: S -> Type} (a :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> TermCont s (Term s a, Term s (PBuiltinList a))
pheadTailC Term s (PBuiltinList a)
ell = (((Term s a, Term s (PBuiltinList a)) -> Term s r) -> Term s r)
-> TermCont s (Term s a, Term s (PBuiltinList a))
forall a (s :: S) (r :: S -> Type).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (Term s (PBuiltinList a)
-> (Term s a -> Term s (PBuiltinList a) -> Term s r) -> Term s r
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
Term s (PBuiltinList a)
-> (Term s a -> Term s (PBuiltinList a) -> Term s b) -> Term s b
pheadTailBuiltin Term s (PBuiltinList a)
ell ((Term s a -> Term s (PBuiltinList a) -> Term s r) -> Term s r)
-> (((Term s a, Term s (PBuiltinList a)) -> Term s r)
    -> Term s a -> Term s (PBuiltinList a) -> Term s r)
-> ((Term s a, Term s (PBuiltinList a)) -> Term s r)
-> Term s r
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Term s a, Term s (PBuiltinList a)) -> Term s r)
-> Term s a -> Term s (PBuiltinList a) -> Term s r
forall a b c. ((a, b) -> c) -> a -> b -> c
curry)

liftCompareOp ::
  forall (s :: S).
  (forall (s' :: S). Term s' PInteger -> Term s' PInteger -> Term s' PBool) ->
  Term s PRationalData ->
  Term s PRationalData ->
  Term s PBool
liftCompareOp :: forall (s :: S).
(forall (s' :: S).
 Term s' PInteger -> Term s' PInteger -> Term s' PBool)
-> Term s PRationalData -> Term s PRationalData -> Term s PBool
liftCompareOp forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
f Term s PRationalData
x Term s PRationalData
y = (forall (s' :: S).
 Term s' (PRationalData :--> (PRationalData :--> PBool)))
-> Term s (PRationalData :--> (PRationalData :--> PBool))
forall (a :: S -> Type) (s :: S).
HasCallStack =>
(forall (s' :: S). Term s' a) -> Term s a
phoistAcyclic ((Term s' PRationalData -> Term s' PRationalData -> Term s' PBool)
-> Term s' (PRationalData :--> (PRationalData :--> PBool))
forall a (b :: S -> Type) (s :: S) (c :: S -> Type).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
forall (c :: S -> Type).
HasCallStack =>
(Term s' c -> Term s' PRationalData -> Term s' PBool)
-> Term s' (c :--> (PRationalData :--> PBool))
plam Term s' PRationalData -> Term s' PRationalData -> Term s' PBool
forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
go) Term s (PRationalData :--> (PRationalData :--> PBool))
-> Term s PRationalData -> Term s (PRationalData :--> PBool)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PRationalData
x Term s (PRationalData :--> PBool)
-> Term s PRationalData -> Term s PBool
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PRationalData
y
  where
    go ::
      forall (s' :: S).
      Term s' PRationalData ->
      Term s' PRationalData ->
      Term s' PBool
    go :: forall (s :: S).
Term s PRationalData -> Term s PRationalData -> Term s PBool
go Term s' PRationalData
l Term s' PRationalData
r = TermCont s' (Term s' PBool) -> Term s' PBool
forall (a :: S -> Type) (s :: S). TermCont s (Term s a) -> Term s a
unTermCont (TermCont s' (Term s' PBool) -> Term s' PBool)
-> TermCont s' (Term s' PBool) -> Term s' PBool
forall a b. (a -> b) -> a -> b
$ do
      PRationalData Term s' (PAsData PInteger)
ln Term s' (PAsData PPositive)
ld <- Term s' PRationalData -> TermCont s' (PRationalData s')
forall {r :: S -> Type} (a :: S -> Type) (s :: S).
PlutusType a =>
Term s a -> TermCont s (a s)
pmatchC Term s' PRationalData
l
      PRationalData Term s' (PAsData PInteger)
rn Term s' (PAsData PPositive)
rd <- Term s' PRationalData -> TermCont s' (PRationalData s')
forall {r :: S -> Type} (a :: S -> Type) (s :: S).
PlutusType a =>
Term s a -> TermCont s (a s)
pmatchC Term s' PRationalData
r
      Term s' PBool -> TermCont s' (Term s' PBool)
forall a. a -> TermCont s' a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s' PBool -> TermCont s' (Term s' PBool))
-> Term s' PBool -> TermCont s' (Term s' PBool)
forall a b. (a -> b) -> a -> b
$ Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
f (Term s' (PAsData PInteger) -> Term s' PInteger
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PInteger)
ln Term s' PInteger -> Term s' PInteger -> Term s' PInteger
forall a. Num a => a -> a -> a
* Term s' PPositive -> Term s' PInteger
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
PSubtype a b =>
Term s b -> Term s a
pupcast (Term s' (PAsData PPositive) -> Term s' PPositive
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PPositive)
rd)) (Term s' (PAsData PInteger) -> Term s' PInteger
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PInteger)
rn Term s' PInteger -> Term s' PInteger -> Term s' PInteger
forall a. Num a => a -> a -> a
* Term s' PPositive -> Term s' PInteger
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
PSubtype a b =>
Term s b -> Term s a
pupcast (Term s' (PAsData PPositive) -> Term s' PPositive
forall (a :: S -> Type) (s :: S).
PIsData a =>
Term s (PAsData a) -> Term s a
pfromData Term s' (PAsData PPositive)
ld))

pgcd :: Term s (PInteger :--> PInteger :--> PInteger)
pgcd :: forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd = (forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger)))
-> Term s (PInteger :--> (PInteger :--> PInteger))
forall (a :: S -> Type) (s :: S).
HasCallStack =>
(forall (s' :: S). Term s' a) -> Term s a
phoistAcyclic ((forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger)))
 -> Term s (PInteger :--> (PInteger :--> PInteger)))
-> (forall (s :: S).
    Term s (PInteger :--> (PInteger :--> PInteger)))
-> Term s (PInteger :--> (PInteger :--> PInteger))
forall a b. (a -> b) -> a -> b
$
  (Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
-> Term s' (PInteger :--> (PInteger :--> PInteger))
forall a (b :: S -> Type) (s :: S) (c :: S -> Type).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
forall (c :: S -> Type).
HasCallStack =>
(Term s' c -> Term s' PInteger -> Term s' PInteger)
-> Term s' (c :--> (PInteger :--> PInteger))
plam ((Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
 -> Term s' (PInteger :--> (PInteger :--> PInteger)))
-> (Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
-> Term s' (PInteger :--> (PInteger :--> PInteger))
forall a b. (a -> b) -> a -> b
$ \Term s' PInteger
x' Term s' PInteger
y' -> TermCont s' (Term s' PInteger) -> Term s' PInteger
forall (a :: S -> Type) (s :: S). TermCont s (Term s a) -> Term s a
unTermCont (TermCont s' (Term s' PInteger) -> Term s' PInteger)
-> TermCont s' (Term s' PInteger) -> Term s' PInteger
forall a b. (a -> b) -> a -> b
$ do
    Term s' PInteger
x <- ((Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
-> TermCont s' (Term s' PInteger)
forall a (s :: S) (r :: S -> Type).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
 -> TermCont s' (Term s' PInteger))
-> (Term s' PInteger
    -> (Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
-> Term s' PInteger
-> TermCont s' (Term s' PInteger)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s' PInteger
-> (Term s' PInteger -> Term s' PInteger) -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s' PInteger -> TermCont s' (Term s' PInteger))
-> Term s' PInteger -> TermCont s' (Term s' PInteger)
forall a b. (a -> b) -> a -> b
$ Term s' (PInteger :--> PInteger)
forall (s :: S). Term s (PInteger :--> PInteger)
forall (a :: S -> Type) (s :: S).
PIntegralDomain a =>
Term s (a :--> a)
pabs Term s' (PInteger :--> PInteger)
-> Term s' PInteger -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
x'
    Term s' PInteger
y <- ((Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
-> TermCont s' (Term s' PInteger)
forall a (s :: S) (r :: S -> Type).
((a -> Term s r) -> Term s r) -> TermCont s a
tcont (((Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
 -> TermCont s' (Term s' PInteger))
-> (Term s' PInteger
    -> (Term s' PInteger -> Term s' PInteger) -> Term s' PInteger)
-> Term s' PInteger
-> TermCont s' (Term s' PInteger)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s' PInteger
-> (Term s' PInteger -> Term s' PInteger) -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s a -> (Term s a -> Term s b) -> Term s b
plet (Term s' PInteger -> TermCont s' (Term s' PInteger))
-> Term s' PInteger -> TermCont s' (Term s' PInteger)
forall a b. (a -> b) -> a -> b
$ Term s' (PInteger :--> PInteger)
forall (s :: S). Term s (PInteger :--> PInteger)
forall (a :: S -> Type) (s :: S).
PIntegralDomain a =>
Term s (a :--> a)
pabs Term s' (PInteger :--> PInteger)
-> Term s' PInteger -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
y'
    Term s' PInteger -> TermCont s' (Term s' PInteger)
forall a. a -> TermCont s' a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (Term s' PInteger -> TermCont s' (Term s' PInteger))
-> Term s' PInteger -> TermCont s' (Term s' PInteger)
forall a b. (a -> b) -> a -> b
$
      Term s' PBool
-> Term s' PInteger -> Term s' PInteger -> Term s' PInteger
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s' PInteger
x Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
POrd t =>
Term s t -> Term s t -> Term s PBool
#<= Term s' PInteger
y)
        (Term s' (PInteger :--> (PInteger :--> PInteger))
forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd' Term s' (PInteger :--> (PInteger :--> PInteger))
-> Term s' PInteger -> Term s' (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
y Term s' (PInteger :--> PInteger)
-> Term s' PInteger -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
x)
        (Term s' (PInteger :--> (PInteger :--> PInteger))
forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd' Term s' (PInteger :--> (PInteger :--> PInteger))
-> Term s' PInteger -> Term s' (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
x Term s' (PInteger :--> PInteger)
-> Term s' PInteger -> Term s' PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s' PInteger
y)

-- assumes inputs are non negative and a >= b
pgcd' :: Term s (PInteger :--> PInteger :--> PInteger)
pgcd' :: forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pgcd' = (forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger)))
-> Term s (PInteger :--> (PInteger :--> PInteger))
forall (a :: S -> Type) (s :: S).
HasCallStack =>
(forall (s' :: S). Term s' a) -> Term s a
phoistAcyclic ((forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger)))
 -> Term s (PInteger :--> (PInteger :--> PInteger)))
-> (forall (s :: S).
    Term s (PInteger :--> (PInteger :--> PInteger)))
-> Term s (PInteger :--> (PInteger :--> PInteger))
forall a b. (a -> b) -> a -> b
$ (Term s' (PInteger :--> (PInteger :--> PInteger))
 -> Term s' (PInteger :--> (PInteger :--> PInteger)))
-> Term s' (PInteger :--> (PInteger :--> PInteger))
forall (a :: S -> Type) (b :: S -> Type) (s :: S).
(Term s (a :--> b) -> Term s (a :--> b)) -> Term s (a :--> b)
pfix ((Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
-> Term s' (PInteger :--> (PInteger :--> PInteger))
forall a (b :: S -> Type) (s :: S) (c :: S -> Type).
(PLamN a b s, HasCallStack) =>
(Term s c -> a) -> Term s (c :--> b)
forall (c :: S -> Type).
HasCallStack =>
(Term s' c -> Term s' PInteger -> Term s' PInteger)
-> Term s' (c :--> (PInteger :--> PInteger))
plam ((Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
 -> Term s' (PInteger :--> (PInteger :--> PInteger)))
-> (Term s' (PInteger :--> (PInteger :--> PInteger))
    -> Term s' PInteger -> Term s' PInteger -> Term s' PInteger)
-> Term s' (PInteger :--> (PInteger :--> PInteger))
-> Term s' (PInteger :--> (PInteger :--> PInteger))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Term s' (PInteger :--> (PInteger :--> PInteger))
-> Term s' PInteger -> Term s' PInteger -> Term s' PInteger
forall {s :: S}.
Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s PInteger -> Term s PInteger
f)
  where
    f :: Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s PInteger -> Term s PInteger
f Term s (PInteger :--> (PInteger :--> PInteger))
self Term s PInteger
a Term s PInteger
b =
      Term s PBool
-> Term s PInteger -> Term s PInteger -> Term s PInteger
forall (a :: S -> Type) (s :: S).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s PInteger
b Term s PInteger -> Term s PInteger -> Term s PBool
forall (s' :: S).
Term s' PInteger -> Term s' PInteger -> Term s' PBool
forall (t :: S -> Type) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
0)
        Term s PInteger
a
        (Term s PInteger -> Term s PInteger)
-> Term s PInteger -> Term s PInteger
forall a b. (a -> b) -> a -> b
$ Term s (PInteger :--> (PInteger :--> PInteger))
self Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PInteger
b Term s (PInteger :--> PInteger)
-> Term s PInteger -> Term s PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (PInteger :--> (PInteger :--> PInteger))
forall (s :: S). Term s (PInteger :--> (PInteger :--> PInteger))
pmod Term s (PInteger :--> (PInteger :--> PInteger))
-> Term s PInteger -> Term s (PInteger :--> PInteger)
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PInteger
a Term s (PInteger :--> PInteger)
-> Term s PInteger -> Term s PInteger
forall (s :: S) (a :: S -> Type) (b :: S -> Type).
Term s (a :--> b) -> Term s a -> Term s b
# Term s PInteger
b