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

module Plutarch.Api.V1.Interval (
  PInterval (PInterval),
  PLowerBound (PLowerBound),
  PUpperBound (PUpperBound),
  PExtended (PFinite, PPosInf, PNegInf),
  type PClosure,
) where

import Plutarch.DataRepr (
  DerivePConstantViaData (DerivePConstantViaData),
  PDataFields,
 )
import Plutarch.Prelude
import PlutusLedgerApi.V1.Interval qualified as Plutus

import Plutarch.Lift (
  PConstantDecl (PConstanted),
  PLifted,
  PUnsafeLiftDecl,
 )

type PClosure = PBool

newtype PInterval a (s :: S)
  = PInterval
      ( Term
          s
          ( PDataRecord
              '[ "from" ':= PLowerBound a
               , "to" ':= PUpperBound a
               ]
          )
      )
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: PType) (s :: S) x.
Rep (PInterval a s) x -> PInterval a s
forall (a :: PType) (s :: S) x.
PInterval a s -> Rep (PInterval a s) x
$cto :: forall (a :: PType) (s :: S) x.
Rep (PInterval a s) x -> PInterval a s
$cfrom :: forall (a :: PType) (s :: S) x.
PInterval a s -> Rep (PInterval a s) x
Generic)
  deriving anyclass (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
forall (a :: PType) (s :: S).
PInterval a s -> Term s (PInner (PInterval a))
forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PInterval a))
-> (PInterval a s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PInterval a))
-> (PInterval a s -> Term s b) -> Term s b
$cpmatch' :: forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PInterval a))
-> (PInterval a s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PInterval a s -> Term s (PInner (PInterval a))
$cpcon' :: forall (a :: PType) (s :: S).
PInterval a s -> Term s (PInner (PInterval a))
PlutusType, forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
forall (a :: PType) (s :: S).
Term s (PAsData (PInterval a)) -> Term s (PInterval a)
forall (a :: PType) (s :: S). Term s (PInterval a) -> Term s PData
pdataImpl :: forall (s :: S). Term s (PInterval a) -> Term s PData
$cpdataImpl :: forall (a :: PType) (s :: S). Term s (PInterval a) -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData (PInterval a)) -> Term s (PInterval a)
$cpfromDataImpl :: forall (a :: PType) (s :: S).
Term s (PAsData (PInterval a)) -> Term s (PInterval a)
PIsData, forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
forall (a :: PType) (s :: S).
Term s (PInterval a)
-> Term s (PDataRecord (PFields (PInterval a)))
ptoFields :: forall (s :: S).
Term s (PInterval a)
-> Term s (PDataRecord (PFields (PInterval a)))
$cptoFields :: forall (a :: PType) (s :: S).
Term s (PInterval a)
-> Term s (PDataRecord (PFields (PInterval a)))
PDataFields, forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PClosure) -> PEq t
forall (a :: PType) (s :: S).
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
#== :: forall (s :: S).
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
$c#== :: forall (a :: PType) (s :: S).
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
PEq, forall {a :: PType}. (POrd a, PIsData a) => PEq (PInterval a)
forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> PPartialOrd t
#< :: forall (s :: S).
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
$c#< :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
#<= :: forall (s :: S).
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
$c#<= :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PInterval a) -> Term s (PInterval a) -> Term s PClosure
PPartialOrd, forall (a :: PType).
(POrd a, PIsData a) =>
PPartialOrd (PInterval a)
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PInterval a) -> 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 (PInterval a) -> Term s PString
$cpshow' :: forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PInterval a) -> Term s PString
PShow)
instance DerivePlutusType (PInterval a) where type DPTStrat _ = PlutusTypeData

instance
  (PLiftData a) =>
  PUnsafeLiftDecl (PInterval a)
  where
  type PLifted (PInterval a) = (Plutus.Interval (PLifted a))
deriving via
  (DerivePConstantViaData (Plutus.Interval a) (PInterval (PConstanted a)))
  instance
    (PConstantData a) =>
    PConstantDecl (Plutus.Interval a)

newtype PLowerBound a (s :: S)
  = PLowerBound
      ( Term
          s
          ( PDataRecord
              '[ "_0" ':= PExtended a
               , "_1" ':= PClosure
               ]
          )
      )
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: PType) (s :: S) x.
Rep (PLowerBound a s) x -> PLowerBound a s
forall (a :: PType) (s :: S) x.
PLowerBound a s -> Rep (PLowerBound a s) x
$cto :: forall (a :: PType) (s :: S) x.
Rep (PLowerBound a s) x -> PLowerBound a s
$cfrom :: forall (a :: PType) (s :: S) x.
PLowerBound a s -> Rep (PLowerBound a s) x
Generic)
  deriving anyclass (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
forall (a :: PType) (s :: S).
PLowerBound a s -> Term s (PInner (PLowerBound a))
forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PLowerBound a))
-> (PLowerBound a s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PLowerBound a))
-> (PLowerBound a s -> Term s b) -> Term s b
$cpmatch' :: forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PLowerBound a))
-> (PLowerBound a s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PLowerBound a s -> Term s (PInner (PLowerBound a))
$cpcon' :: forall (a :: PType) (s :: S).
PLowerBound a s -> Term s (PInner (PLowerBound a))
PlutusType, forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
forall (a :: PType) (s :: S).
Term s (PAsData (PLowerBound a)) -> Term s (PLowerBound a)
forall (a :: PType) (s :: S).
Term s (PLowerBound a) -> Term s PData
pdataImpl :: forall (s :: S). Term s (PLowerBound a) -> Term s PData
$cpdataImpl :: forall (a :: PType) (s :: S).
Term s (PLowerBound a) -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData (PLowerBound a)) -> Term s (PLowerBound a)
$cpfromDataImpl :: forall (a :: PType) (s :: S).
Term s (PAsData (PLowerBound a)) -> Term s (PLowerBound a)
PIsData, forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
forall (a :: PType) (s :: S).
Term s (PLowerBound a)
-> Term s (PDataRecord (PFields (PLowerBound a)))
ptoFields :: forall (s :: S).
Term s (PLowerBound a)
-> Term s (PDataRecord (PFields (PLowerBound a)))
$cptoFields :: forall (a :: PType) (s :: S).
Term s (PLowerBound a)
-> Term s (PDataRecord (PFields (PLowerBound a)))
PDataFields, forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PClosure) -> PEq t
forall (a :: PType) (s :: S).
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
#== :: forall (s :: S).
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
$c#== :: forall (a :: PType) (s :: S).
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
PEq, forall {a :: PType}. (POrd a, PIsData a) => PEq (PLowerBound a)
forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> PPartialOrd t
#< :: forall (s :: S).
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
$c#< :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
#<= :: forall (s :: S).
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
$c#<= :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PLowerBound a) -> Term s (PLowerBound a) -> Term s PClosure
PPartialOrd, forall (a :: PType).
(POrd a, PIsData a) =>
PPartialOrd (PLowerBound a)
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PLowerBound a) -> 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 (PLowerBound a) -> Term s PString
$cpshow' :: forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PLowerBound a) -> Term s PString
PShow)
instance DerivePlutusType (PLowerBound a) where type DPTStrat _ = PlutusTypeData

instance
  (PLiftData a) =>
  PUnsafeLiftDecl (PLowerBound a)
  where
  type PLifted (PLowerBound a) = (Plutus.LowerBound (PLifted a))
deriving via
  (DerivePConstantViaData (Plutus.LowerBound a) (PLowerBound (PConstanted a)))
  instance
    (PConstantData a) =>
    PConstantDecl (Plutus.LowerBound a)

newtype PUpperBound a (s :: S)
  = PUpperBound
      ( Term
          s
          ( PDataRecord
              '[ "_0" ':= PExtended a
               , "_1" ':= PClosure
               ]
          )
      )
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: PType) (s :: S) x.
Rep (PUpperBound a s) x -> PUpperBound a s
forall (a :: PType) (s :: S) x.
PUpperBound a s -> Rep (PUpperBound a s) x
$cto :: forall (a :: PType) (s :: S) x.
Rep (PUpperBound a s) x -> PUpperBound a s
$cfrom :: forall (a :: PType) (s :: S) x.
PUpperBound a s -> Rep (PUpperBound a s) x
Generic)
  deriving anyclass (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
forall (a :: PType) (s :: S).
PUpperBound a s -> Term s (PInner (PUpperBound a))
forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PUpperBound a))
-> (PUpperBound a s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PUpperBound a))
-> (PUpperBound a s -> Term s b) -> Term s b
$cpmatch' :: forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PUpperBound a))
-> (PUpperBound a s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PUpperBound a s -> Term s (PInner (PUpperBound a))
$cpcon' :: forall (a :: PType) (s :: S).
PUpperBound a s -> Term s (PInner (PUpperBound a))
PlutusType, forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
forall (a :: PType) (s :: S).
Term s (PAsData (PUpperBound a)) -> Term s (PUpperBound a)
forall (a :: PType) (s :: S).
Term s (PUpperBound a) -> Term s PData
pdataImpl :: forall (s :: S). Term s (PUpperBound a) -> Term s PData
$cpdataImpl :: forall (a :: PType) (s :: S).
Term s (PUpperBound a) -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData (PUpperBound a)) -> Term s (PUpperBound a)
$cpfromDataImpl :: forall (a :: PType) (s :: S).
Term s (PAsData (PUpperBound a)) -> Term s (PUpperBound a)
PIsData, forall (a :: PType).
(forall (s :: S). Term s a -> Term s (PDataRecord (PFields a)))
-> PDataFields a
forall (a :: PType) (s :: S).
Term s (PUpperBound a)
-> Term s (PDataRecord (PFields (PUpperBound a)))
ptoFields :: forall (s :: S).
Term s (PUpperBound a)
-> Term s (PDataRecord (PFields (PUpperBound a)))
$cptoFields :: forall (a :: PType) (s :: S).
Term s (PUpperBound a)
-> Term s (PDataRecord (PFields (PUpperBound a)))
PDataFields, forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PClosure) -> PEq t
forall (a :: PType) (s :: S).
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
#== :: forall (s :: S).
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
$c#== :: forall (a :: PType) (s :: S).
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
PEq, forall {a :: PType}. (POrd a, PIsData a) => PEq (PUpperBound a)
forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> PPartialOrd t
#< :: forall (s :: S).
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
$c#< :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
#<= :: forall (s :: S).
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
$c#<= :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PUpperBound a) -> Term s (PUpperBound a) -> Term s PClosure
PPartialOrd, forall (a :: PType).
(POrd a, PIsData a) =>
PPartialOrd (PUpperBound a)
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PUpperBound a) -> 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 (PUpperBound a) -> Term s PString
$cpshow' :: forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PUpperBound a) -> Term s PString
PShow)
instance DerivePlutusType (PUpperBound a) where type DPTStrat _ = PlutusTypeData

data PExtended a (s :: S)
  = PNegInf (Term s (PDataRecord '[]))
  | PFinite (Term s (PDataRecord '["_0" ':= a]))
  | PPosInf (Term s (PDataRecord '[]))
  deriving stock (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: PType) (s :: S) x.
Rep (PExtended a s) x -> PExtended a s
forall (a :: PType) (s :: S) x.
PExtended a s -> Rep (PExtended a s) x
$cto :: forall (a :: PType) (s :: S) x.
Rep (PExtended a s) x -> PExtended a s
$cfrom :: forall (a :: PType) (s :: S) x.
PExtended a s -> Rep (PExtended a s) x
Generic)
  deriving anyclass (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
forall (a :: PType) (s :: S).
PExtended a s -> Term s (PInner (PExtended a))
forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PExtended a))
-> (PExtended a s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PExtended a))
-> (PExtended a s -> Term s b) -> Term s b
$cpmatch' :: forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PExtended a))
-> (PExtended a s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PExtended a s -> Term s (PInner (PExtended a))
$cpcon' :: forall (a :: PType) (s :: S).
PExtended a s -> Term s (PInner (PExtended a))
PlutusType, forall (a :: PType).
(forall (s :: S). Term s (PAsData a) -> Term s a)
-> (forall (s :: S). Term s a -> Term s PData) -> PIsData a
forall (a :: PType) (s :: S).
Term s (PAsData (PExtended a)) -> Term s (PExtended a)
forall (a :: PType) (s :: S). Term s (PExtended a) -> Term s PData
pdataImpl :: forall (s :: S). Term s (PExtended a) -> Term s PData
$cpdataImpl :: forall (a :: PType) (s :: S). Term s (PExtended a) -> Term s PData
pfromDataImpl :: forall (s :: S).
Term s (PAsData (PExtended a)) -> Term s (PExtended a)
$cpfromDataImpl :: forall (a :: PType) (s :: S).
Term s (PAsData (PExtended a)) -> Term s (PExtended a)
PIsData, forall (t :: PType).
(forall (s :: S). Term s t -> Term s t -> Term s PClosure) -> PEq t
forall (a :: PType) (s :: S).
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
#== :: forall (s :: S).
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
$c#== :: forall (a :: PType) (s :: S).
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
PEq, forall {a :: PType}. (POrd a, PIsData a) => PEq (PExtended a)
forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
forall (t :: PType).
PEq t
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> (forall (s :: S). Term s t -> Term s t -> Term s PClosure)
-> PPartialOrd t
#< :: forall (s :: S).
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
$c#< :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
#<= :: forall (s :: S).
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
$c#<= :: forall (a :: PType) (s :: S).
(POrd a, PIsData a) =>
Term s (PExtended a) -> Term s (PExtended a) -> Term s PClosure
PPartialOrd, forall (a :: PType).
(POrd a, PIsData a) =>
PPartialOrd (PExtended a)
forall (t :: PType). PPartialOrd t -> POrd t
POrd, forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PExtended a) -> 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 (PExtended a) -> Term s PString
$cpshow' :: forall (a :: PType) (s :: S).
(PIsData a, PShow a) =>
Bool -> Term s (PExtended a) -> Term s PString
PShow)
instance DerivePlutusType (PExtended a) where type DPTStrat _ = PlutusTypeData

instance
  (PLiftData a) =>
  PUnsafeLiftDecl (PUpperBound a)
  where
  type PLifted (PUpperBound a) = (Plutus.UpperBound (PLifted a))
deriving via
  (DerivePConstantViaData (Plutus.UpperBound a) (PUpperBound (PConstanted a)))
  instance
    (PConstantData a) =>
    PConstantDecl (Plutus.UpperBound a)