{-# LANGUAGE RankNTypes #-}
-- Needed to 'link' Ordering and POrdering
{-# OPTIONS_GHC -Wno-orphans #-}

{- |
 Module: Plutarch.Extra.Ord
 Copyright: (C) Liqwid Labs 2022
 License: Apache 2.0
 Maintainer: Koz Ross <[email protected]>
 Portability: GHC only
 Stability: Experimental

 Ordering-related helpers and functionality.
-}
module Plutarch.Extra.Ord (
  -- * Types
  POrdering (..),
  PComparator,

  -- * Functions

  -- ** POrd functions
  pmax,
  pmin,

  -- ** Creating comparators
  pfromOrd,
  pfromOrdBy,

  -- ** Transforming comparators
  pmapComparator,
  preverseComparator,

  -- ** Using comparators

  -- *** Basic
  pcompareBy,
  pequateBy,
  pleqBy,
  plessThanBy,
  pgeqBy,
  pgreaterThanBy,
  pmaxBy,
  pminBy,

  -- *** Data structures

  -- **** Map
  pleqMapBy,
  plessThanMapBy,
  pgeqMapBy,
  pgreaterThanMapBy,

  -- **** 'PValue'
  pleqValueBy,
  plessThanValueBy,
  pgeqValueBy,
  pgreaterThanValueBy,

  -- *** Sortedness checking
  pisSortedBy,

  -- *** Uniqueness checking
  pallUnique,
  pallUniqueBy,
  ptryAllUnique,
  ptryAllUniqueBy,

  -- *** Sorted merging
  ptryMerge,
  ptryMergeBy,

  -- *** Sorting
  psort,
  psortBy,

  -- *** Nubbing
  pnubSort,
  pnubSortBy,

  -- *** Inserting
  pinsertUniqueBy,
) where

import Data.Semigroup (Semigroup (stimes), stimesIdempotentMonoid)
import Plutarch.Api.V1.AssocMap (KeyGuarantees (Sorted), PMap)
import Plutarch.Api.V1.Value (
  AmountGuarantees,
  PCurrencySymbol,
  PTokenName,
  PValue,
 )
import Plutarch.Bool (pif')
import Plutarch.Extra.List (phandleList, precListLookahead)
import Plutarch.Extra.Map (phandleMin)
import Plutarch.Extra.Maybe (ptraceIfNothing)
import Plutarch.Internal.PlutusType (PlutusType (pcon', pmatch'))
import Plutarch.Lift (
  PConstantDecl (
    PConstantRepr,
    PConstanted,
    pconstantFromRepr,
    pconstantToRepr
  ),
  PUnsafeLiftDecl (PLifted),
 )
import Plutarch.List (pconvertLists)
import Plutarch.Unsafe (punsafeCoerce)

{- | Return the minimum of two arguments.

  @since 3.15.4
-}
pmin ::
  forall (a :: S -> Type) (s :: S).
  POrd a =>
  Term s (a :--> a :--> a)
pmin :: forall (a :: PType) (s :: S). POrd a => Term s (a :--> (a :--> a))
pmin = 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 a
x Term s a
y ->
  forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
    # (x #<= y)
    # x
    # y

{- | Return the maximum of two arguments.

  @since 3.15.4
-}
pmax ::
  forall (a :: S -> Type) (s :: S).
  POrd a =>
  Term s (a :--> a :--> a)
pmax :: forall (a :: PType) (s :: S). POrd a => Term s (a :--> (a :--> a))
pmax = 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 a
x Term s a
y ->
  forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
    # (x #<= y)
    # y
    # x

{- | A representation of a comparison at the Plutarch level. Equivalent to
 'Ordering' in Haskell.

 @since 3.6.0
-}
data POrdering (s :: S)
  = -- | Indicates a less-than relationship.
    --
    -- @since 3.6.0
    PLT
  | -- | Indicates equality.
    --
    -- @since 3.6.0
    PEQ
  | -- | Indicates a greater-than relationship.
    --
    -- @since 3.6.0
    PGT
  deriving stock
    ( -- | @since 3.6.0
      Int -> POrdering s -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (s :: S). Int -> POrdering s -> ShowS
forall (s :: S). [POrdering s] -> ShowS
forall (s :: S). POrdering s -> String
showList :: [POrdering s] -> ShowS
$cshowList :: forall (s :: S). [POrdering s] -> ShowS
show :: POrdering s -> String
$cshow :: forall (s :: S). POrdering s -> String
showsPrec :: Int -> POrdering s -> ShowS
$cshowsPrec :: forall (s :: S). Int -> POrdering s -> ShowS
Show
    , -- | @since 3.6.0
      forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (s :: S) x. Rep (POrdering s) x -> POrdering s
forall (s :: S) x. POrdering s -> Rep (POrdering s) x
$cto :: forall (s :: S) x. Rep (POrdering s) x -> POrdering s
$cfrom :: forall (s :: S) x. POrdering s -> Rep (POrdering s) x
Generic
    )
  deriving anyclass
    ( -- | @since 3.6.0
      forall (s :: S). Bool -> Term s POrdering -> 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 POrdering -> Term s PString
$cpshow' :: forall (s :: S). Bool -> Term s POrdering -> Term s PString
PShow
    , -- | @since 3.6.0
      PEq POrdering
forall (s :: S).
Term s POrdering -> Term s POrdering -> 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 POrdering -> Term s POrdering -> Term s PBool
$c#< :: forall (s :: S).
Term s POrdering -> Term s POrdering -> Term s PBool
#<= :: forall (s :: S).
Term s POrdering -> Term s POrdering -> Term s PBool
$c#<= :: forall (s :: S).
Term s POrdering -> Term s POrdering -> Term s PBool
PPartialOrd
    , -- | @since 3.6.0
      PPartialOrd POrdering
forall (t :: PType). PPartialOrd t -> POrd t
POrd
    )

-- | @since 3.6.0
instance PUnsafeLiftDecl POrdering where
  type PLifted POrdering = Ordering

-- | @since 3.6.0
instance PConstantDecl Ordering where
  type PConstantRepr Ordering = Integer
  type PConstanted Ordering = POrdering
  pconstantToRepr :: Ordering -> PConstantRepr Ordering
pconstantToRepr = \case
    Ordering
LT -> Integer
0
    Ordering
EQ -> Integer
1
    Ordering
GT -> Integer
2
  pconstantFromRepr :: PConstantRepr Ordering -> Maybe Ordering
pconstantFromRepr = \case
    PConstantRepr Ordering
0 -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Ordering
LT
    PConstantRepr Ordering
1 -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Ordering
EQ
    PConstantRepr Ordering
2 -> forall (f :: Type -> Type) a. Applicative f => a -> f a
pure Ordering
GT
    PConstantRepr Ordering
_ -> forall a. Maybe a
Nothing

-- | @since 3.6.0
instance PlutusType POrdering where
  type PInner POrdering = PInteger
  pcon' :: forall (s :: S). POrdering s -> Term s (PInner POrdering)
pcon' = \case
    POrdering s
PLT -> Term s PInteger
0
    POrdering s
PEQ -> Term s PInteger
1
    POrdering s
PGT -> Term s PInteger
2
  pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner POrdering) -> (POrdering s -> Term s b) -> Term s b
pmatch' Term s (PInner POrdering)
x POrdering s -> Term s b
f =
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (Term s (PInner POrdering)
x forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
0)
      (POrdering s -> Term s b
f forall (s :: S). POrdering s
PLT)
      ( forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
          (Term s (PInner POrdering)
x forall (t :: PType) (s :: S).
PEq t =>
Term s t -> Term s t -> Term s PBool
#== Term s PInteger
1)
          (POrdering s -> Term s b
f forall (s :: S). POrdering s
PEQ)
          (POrdering s -> Term s b
f forall (s :: S). POrdering s
PGT)
      )

-- | @since 3.6.0
instance PEq POrdering where
  Term s POrdering
x #== :: forall (s :: S).
Term s POrdering -> Term s POrdering -> Term s PBool
#== Term s POrdering
y = forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s POrdering
x 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 POrdering
y

-- | @since 3.6.0
instance Semigroup (Term s POrdering) where
  Term s POrdering
x <> :: Term s POrdering -> Term s POrdering -> Term s POrdering
<> Term s POrdering
y = forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s POrdering
x forall (t :: PType) (s :: S).
PPartialOrd t =>
Term s t -> Term s t -> Term s PBool
#< Term s PInteger
2) (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 POrdering
x forall a. Num a => a -> a -> a
* forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s POrdering
y) Term s POrdering
x
  stimes :: forall b. Integral b => b -> Term s POrdering -> Term s POrdering
stimes = forall b a. (Integral b, Monoid a) => b -> a -> a
stimesIdempotentMonoid

-- | @since 3.6.0
instance Monoid (Term s POrdering) where
  mempty :: Term s POrdering
mempty = forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). POrdering s
PEQ

-- | @since 3.6.0
data PComparator (a :: S -> Type) (s :: S) = PComparator
  { forall (a :: PType) (s :: S).
PComparator a s -> Term s (a :--> (a :--> PBool))
pcomparatorEq :: Term s (a :--> a :--> PBool)
  , forall (a :: PType) (s :: S).
PComparator a s -> Term s (a :--> (a :--> PBool))
pcomparatorLe :: Term s (a :--> a :--> PBool)
  }
  deriving stock
    ( -- | @since 3.6.0
      forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (a :: PType) (s :: S) x.
Rep (PComparator a s) x -> PComparator a s
forall (a :: PType) (s :: S) x.
PComparator a s -> Rep (PComparator a s) x
$cto :: forall (a :: PType) (s :: S) x.
Rep (PComparator a s) x -> PComparator a s
$cfrom :: forall (a :: PType) (s :: S) x.
PComparator a s -> Rep (PComparator a s) x
Generic
    )
  deriving anyclass
    ( -- | @since 3.6.0
      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).
PComparator a s -> Term s (PInner (PComparator a))
forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PComparator a))
-> (PComparator a s -> Term s b) -> Term s b
pmatch' :: forall (s :: S) (b :: PType).
Term s (PInner (PComparator a))
-> (PComparator a s -> Term s b) -> Term s b
$cpmatch' :: forall (a :: PType) (s :: S) (b :: PType).
Term s (PInner (PComparator a))
-> (PComparator a s -> Term s b) -> Term s b
pcon' :: forall (s :: S). PComparator a s -> Term s (PInner (PComparator a))
$cpcon' :: forall (a :: PType) (s :: S).
PComparator a s -> Term s (PInner (PComparator a))
PlutusType
    )

-- | @since 3.6.0
instance DerivePlutusType (PComparator a) where
  type DPTStrat _ = PlutusTypeScott

-- TODO: Semigroup, Monoid

{- | Given a type with a 'POrd' instance, construct a 'PComparator' from that
 instance.

 @since 3.6.0
-}
pfromOrd ::
  forall (a :: S -> Type) (s :: S).
  (POrd a) =>
  Term s (PComparator a)
pfromOrd :: forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd =
  forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
Term s (a :--> (a :--> PBool))
-> Term s (a :--> (a :--> PBool)) -> PComparator a s
PComparator (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).
PEq t =>
Term s t -> Term s t -> Term s PBool
(#==)) forall a b. (a -> b) -> a -> b
$
    forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic (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
(#<=))

{- | As 'pfromOrd', but instead uses a projection function into the 'POrd'
 instance to construct the 'PComparator'. Allows other \'-by\' behaviours.

 @since 3.6.0
-}
pfromOrdBy ::
  forall (a :: S -> Type) (b :: S -> Type) (s :: S).
  (POrd a) =>
  Term s ((b :--> a) :--> PComparator b)
pfromOrdBy :: forall (a :: PType) (b :: PType) (s :: S).
POrd a =>
Term s ((b :--> a) :--> PComparator b)
pfromOrdBy = 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 (b :--> a)
f ->
    forall (a :: PType) (b :: PType) (s :: S).
Term s ((b :--> a) :--> (PComparator a :--> PComparator b))
pmapComparator forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (b :--> a)
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a

{- | Given a projection from a type to another type which we have a
 'PComparator' for, construct a new 'PComparator'.

 @since 3.6.0
-}
pmapComparator ::
  forall (a :: S -> Type) (b :: S -> Type) (s :: S).
  Term s ((b :--> a) :--> PComparator a :--> PComparator b)
pmapComparator :: forall (a :: PType) (b :: PType) (s :: S).
Term s ((b :--> a) :--> (PComparator a :--> PComparator b))
pmapComparator = 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 (b :--> a)
f Term s (PComparator a)
cmp ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
Term s (a :--> (a :--> PBool))
-> Term s (a :--> (a :--> PBool)) -> PComparator a s
PComparator (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 b
x Term s b
y -> Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (Term s (b :--> a)
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s b
x) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (Term s (b :--> a)
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s b
y)) 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 b
x Term s b
y -> Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (Term s (b :--> a)
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s b
x) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# (Term s (b :--> a)
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s b
y)

{- | Reverses the ordering described by a 'PComparator'.

 @since 3.6.0
-}
preverseComparator ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> PComparator a)
preverseComparator :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> PComparator a)
preverseComparator = 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 (PComparator a)
cmp ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
Term s (a :--> (a :--> PBool))
-> Term s (a :--> (a :--> PBool)) -> PComparator a s
PComparator Term s (a :--> (a :--> PBool))
peq 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 a
x Term s a
y -> Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x

{- | \'Runs\' a 'PComparator'.

 @since 3.6.0
-}
pcompareBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> POrdering)
pcompareBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> POrdering)))
pcompareBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)
        (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). POrdering s
PEQ)
        ( forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
            (Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)
            (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). POrdering s
PLT)
            (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). POrdering s
PGT)
        )

{- | Uses a 'PComparator' for an equality check.

 @since 3.6.0
-}
pequateBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> PBool)
pequateBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pequateBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
_) -> Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y

{- | Uses a 'PComparator' for a less-than-or-equals check.

 @since 3.6.0
-}
pleqBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> PBool)
pleqBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
_ Term s (a :--> (a :--> PBool))
ple) -> Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y

{- | Uses a 'PComparator' for a less-than check.

 @since 3.9.0
-}
plessThanBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> PBool)
plessThanBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
plessThanBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      (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
#$ Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y) forall (s :: S). Term s PBool -> Term s PBool -> Term s PBool
#&& (Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)

{- | Uses a 'PComparator' for a greater-than-or-equals check.

 @since 3.6.0
-}
pgeqBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> PBool)
pgeqBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pgeqBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)
        (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue)
        (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
#$ Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)

{- | Uses a 'PComparator' for a greater-than check.

 @since 3.9.0
-}
pgreaterThanBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> PBool)
pgreaterThanBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pgreaterThanBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
peq Term s (a :--> (a :--> PBool))
ple) ->
      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
#$ (Term s (a :--> (a :--> PBool))
peq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y) forall (s :: S). Term s PBool -> Term s PBool -> Term s PBool
#|| (Term s (a :--> (a :--> PBool))
ple forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y)

{- | Uses a 'PComparator' to return the minimum of two arguments

   @since 3.15.4
-}
pminBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> a)
pminBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> a)))
pminBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
_ Term s (a :--> (a :--> PBool))
ple) ->
      forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
        # (ple # x # y)
        # x
        # y

{- | Uses a 'PComparator' to return the maximum of two arguments

     @since 3.15.4
-}
pmaxBy ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> a :--> a :--> a)
pmaxBy :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> a)))
pmaxBy = 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 (PComparator a)
cmp Term s a
x Term s a
y ->
    forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch Term s (PComparator a)
cmp forall a b. (a -> b) -> a -> b
$ \(PComparator Term s (a :--> (a :--> PBool))
_ Term s (a :--> (a :--> PBool))
ple) ->
      forall (s :: S) (a :: PType).
Term s (PBool :--> (a :--> (a :--> a)))
pif'
        # (ple # x # y)
        # y
        # x

{- | Compares two sorted 'PMap's using a 'PComparator' on their values.
 Specifically, this returns 'PTrue' if, for any key in both argument 'PMap's,
 the value associated with this key in the first 'PMap' compares
 less-than-or-equal-to its corresponding value in the second 'PMap'; in any
 other case, this returns 'PFalse'.

 @since 3.9.0
-}
pleqMapBy ::
  forall (k :: S -> Type) (v :: S -> Type) (s :: S).
  (PIsData k, PIsData v, POrd k) =>
  Term s (PComparator v :--> PMap 'Sorted k v :--> PMap 'Sorted k v :--> PBool)
pleqMapBy :: forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pleqMapBy = 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 (PComparator v)
cmp Term s (PMap 'Sorted k v)
m1 Term s (PMap 'Sorted k v)
m2 ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1 Term s v
v1 Term s (PMap 'Sorted k v)
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2 Term s v
v2 Term s (PMap 'Sorted k v)
kv2 ->
        (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 (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
-> Term s (PComparator v :--> (v :--> (v :--> PBool)))
-> Term s (PComparator v)
-> Term s k
-> Term s k
-> Term s v
-> Term s v
-> Term s (PMap 'Sorted k v)
-> Term s (PMap 'Sorted k v)
-> Term s PBool
pcmpMap) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2

{- | As 'pleqMapBy', but the \'expected comparison\' over values is strictly
 less than, rather than less-than-or-equal-to.

 @since 3.9.0
-}
plessThanMapBy ::
  forall (k :: S -> Type) (v :: S -> Type) (s :: S).
  (PIsData k, PIsData v, POrd k) =>
  Term s (PComparator v :--> PMap 'Sorted k v :--> PMap 'Sorted k v :--> PBool)
plessThanMapBy :: forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
plessThanMapBy = 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 (PComparator v)
cmp Term s (PMap 'Sorted k v)
m1 Term s (PMap 'Sorted k v)
m2 ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1 Term s v
v1 Term s (PMap 'Sorted k v)
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2 Term s v
v2 Term s (PMap 'Sorted k v)
kv2 ->
        (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 (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
-> Term s (PComparator v :--> (v :--> (v :--> PBool)))
-> Term s (PComparator v)
-> Term s k
-> Term s k
-> Term s v
-> Term s v
-> Term s (PMap 'Sorted k v)
-> Term s (PMap 'Sorted k v)
-> Term s PBool
pcmpMap) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
plessThanBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2

{- | As 'pleqMapBy', but the \'expected comparison\' over values is
 greater-than-or-equal-to, rather than less-than-or-equal-to.

 @since 3.9.0
-}
pgeqMapBy ::
  forall (k :: S -> Type) (v :: S -> Type) (s :: S).
  (PIsData k, PIsData v, POrd k) =>
  Term s (PComparator v :--> PMap 'Sorted k v :--> PMap 'Sorted k v :--> PBool)
pgeqMapBy :: forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pgeqMapBy = 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 (PComparator v)
cmp Term s (PMap 'Sorted k v)
m1 Term s (PMap 'Sorted k v)
m2 ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1 Term s v
v1 Term s (PMap 'Sorted k v)
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2 Term s v
v2 Term s (PMap 'Sorted k v)
kv2 ->
        (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 (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
-> Term s (PComparator v :--> (v :--> (v :--> PBool)))
-> Term s (PComparator v)
-> Term s k
-> Term s k
-> Term s v
-> Term s v
-> Term s (PMap 'Sorted k v)
-> Term s (PMap 'Sorted k v)
-> Term s PBool
pcmpMap) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pgeqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2

{- | As 'pleqMapBy', but the \'expected comparison\' over values is strictly
 greater than, rather than less-than-or-equal-to.

 @since 3.9.0
-}
pgreaterThanMapBy ::
  forall (k :: S -> Type) (v :: S -> Type) (s :: S).
  (PIsData k, PIsData v, POrd k) =>
  Term s (PComparator v :--> PMap 'Sorted k v :--> PMap 'Sorted k v :--> PBool)
pgreaterThanMapBy :: forall (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pgreaterThanMapBy = 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 (PComparator v)
cmp Term s (PMap 'Sorted k v)
m1 Term s (PMap 'Sorted k v)
m2 ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1 Term s v
v1 Term s (PMap 'Sorted k v)
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
m2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2 Term s v
v2 Term s (PMap 'Sorted k v)
kv2 ->
        (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 (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
-> Term s (PComparator v :--> (v :--> (v :--> PBool)))
-> Term s (PComparator v)
-> Term s k
-> Term s k
-> Term s v
-> Term s v
-> Term s (PMap 'Sorted k v)
-> Term s (PMap 'Sorted k v)
-> Term s PBool
pcmpMap) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pgreaterThanBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2

{- | Compares two 'PValue's based on their \'inner map\' 'pleqThanMapBy'. More
 precisely, this returns 'PTrue' if, for any 'PCurrencySymbol' with a mapping
 in both 'PValue's, their corresponding \'inner maps\' all give 'PTrue' using
 'pleqMapBy' with the given comparator.

 @since 3.9.0
-}
pleqValueBy ::
  forall (amount :: AmountGuarantees) (s :: S).
  Term s (PComparator PInteger :--> PValue 'Sorted amount :--> PValue 'Sorted amount :--> PBool)
pleqValueBy :: forall (amount :: AmountGuarantees) (s :: S).
Term
  s
  (PComparator PInteger
   :--> (PValue 'Sorted amount
         :--> (PValue 'Sorted amount :--> PBool)))
pleqValueBy = 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 (PComparator PInteger)
cmp Term s (PValue 'Sorted amount)
pval Term s (PValue 'Sorted amount)
pval' ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1 Term s (PMap 'Sorted PTokenName PInteger)
v1 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval') (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2 Term s (PMap 'Sorted PTokenName PInteger)
v2 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 ->
        (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 (s :: S).
Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
-> Term
     s
     (PComparator PInteger
      :--> (PMap 'Sorted PTokenName PInteger
            :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
-> Term s (PComparator PInteger)
-> Term s PCurrencySymbol
-> Term s PCurrencySymbol
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term s PBool
pcmpValue) 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, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pleqMapBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2

{- | As 'pleqValueBy', except the \'comparison function\' used is
 'plessThanMapBy'.

 @since 3.9.0
-}
plessThanValueBy ::
  forall (amount :: AmountGuarantees) (s :: S).
  Term s (PComparator PInteger :--> PValue 'Sorted amount :--> PValue 'Sorted amount :--> PBool)
plessThanValueBy :: forall (amount :: AmountGuarantees) (s :: S).
Term
  s
  (PComparator PInteger
   :--> (PValue 'Sorted amount
         :--> (PValue 'Sorted amount :--> PBool)))
plessThanValueBy = 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 (PComparator PInteger)
cmp Term s (PValue 'Sorted amount)
pval Term s (PValue 'Sorted amount)
pval' ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1 Term s (PMap 'Sorted PTokenName PInteger)
v1 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval') (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2 Term s (PMap 'Sorted PTokenName PInteger)
v2 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 ->
        (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 (s :: S).
Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
-> Term
     s
     (PComparator PInteger
      :--> (PMap 'Sorted PTokenName PInteger
            :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
-> Term s (PComparator PInteger)
-> Term s PCurrencySymbol
-> Term s PCurrencySymbol
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term s PBool
pcmpValue) 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, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
plessThanMapBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2

{- | As 'pleqValueBy', except the \'comparison function\' used is 'pgeqMapBy'.

 @since 3.9.0
-}
pgeqValueBy ::
  forall (amount :: AmountGuarantees) (s :: S).
  Term s (PComparator PInteger :--> PValue 'Sorted amount :--> PValue 'Sorted amount :--> PBool)
pgeqValueBy :: forall (amount :: AmountGuarantees) (s :: S).
Term
  s
  (PComparator PInteger
   :--> (PValue 'Sorted amount
         :--> (PValue 'Sorted amount :--> PBool)))
pgeqValueBy = 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 (PComparator PInteger)
cmp Term s (PValue 'Sorted amount)
pval Term s (PValue 'Sorted amount)
pval' ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1 Term s (PMap 'Sorted PTokenName PInteger)
v1 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval') (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2 Term s (PMap 'Sorted PTokenName PInteger)
v2 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 ->
        (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 (s :: S).
Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
-> Term
     s
     (PComparator PInteger
      :--> (PMap 'Sorted PTokenName PInteger
            :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
-> Term s (PComparator PInteger)
-> Term s PCurrencySymbol
-> Term s PCurrencySymbol
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term s PBool
pcmpValue) 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, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pgeqMapBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2

{- | As 'pleqValueBy', except the \'comparison function\' used is
 'pgreaterThanMapBy'.

 @since 3.9.0
-}
pgreaterThanValueBy ::
  forall (amount :: AmountGuarantees) (s :: S).
  Term s (PComparator PInteger :--> PValue 'Sorted amount :--> PValue 'Sorted amount :--> PBool)
pgreaterThanValueBy :: forall (amount :: AmountGuarantees) (s :: S).
Term
  s
  (PComparator PInteger
   :--> (PValue 'Sorted amount
         :--> (PValue 'Sorted amount :--> PBool)))
pgreaterThanValueBy = 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 (PComparator PInteger)
cmp Term s (PValue 'Sorted amount)
pval Term s (PValue 'Sorted amount)
pval' ->
    forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1 Term s (PMap 'Sorted PTokenName PInteger)
v1 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 ->
      forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin (forall (s :: S) (a :: PType). Term s a -> Term s (PInner a)
pto Term s (PValue 'Sorted amount)
pval') (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2 Term s (PMap 'Sorted PTokenName PInteger)
v2 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 ->
        (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 (s :: S).
Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
-> Term
     s
     (PComparator PInteger
      :--> (PMap 'Sorted PTokenName PInteger
            :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
-> Term s (PComparator PInteger)
-> Term s PCurrencySymbol
-> Term s PCurrencySymbol
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term s PBool
pcmpValue) 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, POrd k) =>
Term
  s
  (PComparator v
   :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool)))
pgreaterThanMapBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2

{- | Verifies that a list-like structure is ordered according to the
 'PComparator' argument.

 @since 3.6.0
-}
pisSortedBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> PBool)
pisSortedBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PBool))
pisSortedBy = 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 (PComparator a)
cmp ->
    forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
(Term s (a :--> (ell a :--> r))
 -> Term s a -> Term s a -> Term s (ell a) -> Term s r)
-> (Term s a -> Term s r) -> Term s r -> Term s (ell a :--> r)
precListLookahead (forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> PBool))
-> Term s' a
-> Term s' a
-> Term s' (ell a)
-> Term s' PBool
go Term s (PComparator a)
cmp) (forall a b. a -> b -> a
const (forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant Bool
True)) (forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant Bool
True)
  where
    go ::
      forall (s' :: S).
      Term s' (PComparator a) ->
      Term s' (a :--> ell a :--> PBool) ->
      Term s' a ->
      Term s' a ->
      Term s' (ell a) ->
      Term s' PBool
    go :: forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> PBool))
-> Term s' a
-> Term s' a
-> Term s' (ell a)
-> Term s' PBool
go Term s' (PComparator a)
cmp Term s' (a :--> (ell a :--> PBool))
self Term s' a
h Term s' a
peek Term s' (ell a)
rest =
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek)
        (Term s' (a :--> (ell a :--> PBool))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
rest)
        (forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant Bool
False)

{- | Verifies that a list-like structure is both ordered (by the 'POrd' instance
 it's full of) and has no duplicates (by the 'PEq' instance it's full of).
 This can give any of the following results:

 * 'PNothing' if the structure is found to be unsorted;
 * 'PJust' 'PFalse' if the structure contains a duplicate; or
 * 'PJust' 'PTrue' if it is both sorted and contains no duplicates.

 @since 3.6.0
-}
pallUnique ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (POrd a, PElemConstraint ell a, PListLike ell) =>
  Term s (ell a :--> PMaybe PBool)
pallUnique :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(POrd a, PElemConstraint ell a, PListLike ell) =>
Term s (ell a :--> PMaybe PBool)
pallUnique = 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 :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PMaybe PBool))
pallUniqueBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#)

{- | As 'pallUnique', but using a custom 'PComparator' to verify order and
 equality.

 @since 3.6.0
-}
pallUniqueBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> PMaybe PBool)
pallUniqueBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PMaybe PBool))
pallUniqueBy = 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 (PComparator a)
cmp ->
    forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
(Term s (a :--> (ell a :--> r))
 -> Term s a -> Term s a -> Term s (ell a) -> Term s r)
-> (Term s a -> Term s r) -> Term s r -> Term s (ell a :--> r)
precListLookahead (forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> PMaybe PBool))
-> Term s' a
-> Term s' a
-> Term s' (ell a)
-> Term s' (PMaybe PBool)
go Term s (PComparator a)
cmp) (forall a b. a -> b -> a
const forall (s' :: S). Term s' (PMaybe PBool)
success) forall (s' :: S). Term s' (PMaybe PBool)
success
  where
    success :: forall (s' :: S). Term s' (PMaybe PBool)
    success :: forall (s' :: S). Term s' (PMaybe PBool)
success = forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S). Term s a -> PMaybe a s
PJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant forall a b. (a -> b) -> a -> b
$ Bool
True
    go ::
      forall (s' :: S).
      Term s' (PComparator a) ->
      Term s' (a :--> ell a :--> PMaybe PBool) ->
      Term s' a ->
      Term s' a ->
      Term s' (ell a) ->
      Term s' (PMaybe PBool)
    go :: forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> PMaybe PBool))
-> Term s' a
-> Term s' a
-> Term s' (ell a)
-> Term s' (PMaybe PBool)
go Term s' (PComparator a)
cmp Term s' (a :--> (ell a :--> PMaybe PBool))
self Term s' a
h Term s' a
peek Term s' (ell a)
t = forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> POrdering)))
pcompareBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek) forall a b. (a -> b) -> a -> b
$ \case
      POrdering s'
PLT -> Term s' (a :--> (ell a :--> PMaybe PBool))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
t
      POrdering s'
PEQ -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S). Term s a -> PMaybe a s
PJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (p :: PType) (s :: S). PLift p => PLifted p -> Term s p
pconstant forall a b. (a -> b) -> a -> b
$ Bool
False
      POrdering s'
PGT -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (a :: PType) (s :: S). PMaybe a s
PNothing

{- | As 'pallUnique', but errors if the list-like argument is found to be
 unsorted instead of returning 'PNothing'.

 @since 3.6.0
-}
ptryAllUnique ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (POrd a, PElemConstraint ell a, PListLike ell) =>
  Term s (ell a :--> PBool)
ptryAllUnique :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(POrd a, PElemConstraint ell a, PListLike ell) =>
Term s (ell a :--> PBool)
ptryAllUnique = 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 :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PBool))
ptryAllUniqueBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#)

{- | As 'pallUniqueBy', but errors if the list-like argument is found to be
 unsorted instead of returning 'PNothing'.

 @since 3.6.0
-}
ptryAllUniqueBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> PBool)
ptryAllUniqueBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PBool))
ptryAllUniqueBy = 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 (PComparator a)
cmp Term s (ell a)
xs ->
    forall (a :: PType) (s :: S).
Term s PString -> Term s (PMaybe a) -> Term s a
ptraceIfNothing Term s PString
"ptryAllUniqueBy: argument is unordered" forall a b. (a -> b) -> a -> b
$ forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PMaybe PBool))
pallUniqueBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs

{- | Merge two list-like structures, whose contents are sorted by the 'POrd'
 instance for their contents, into one sorted list-like structure. This will
 error if it finds that one of the list-like structures given to it as an
 argument is not sorted.

 @since 3.6.0
-}
ptryMerge ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (POrd a, PElemConstraint ell a, PListLike ell) =>
  Term s (ell a :--> ell a :--> ell a)
ptryMerge :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(POrd a, PElemConstraint ell a, PListLike ell) =>
Term s (ell a :--> (ell a :--> ell a))
ptryMerge = 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 :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> (ell a :--> ell a)))
ptryMergeBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#)

{- | As 'ptryMerge', but instead of using 'POrd' to determine sorting, uses a
 custom 'PComparator'. Will error out if one of the list-like structures given
 as an argument is not sorted according to the custom 'PComparator'.

 @since 3.6.0
-}
ptryMergeBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> ell a :--> ell a)
ptryMergeBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> (ell a :--> ell a)))
ptryMergeBy = 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 (PComparator a)
cmp Term s (ell a)
xs Term s (ell a)
ys ->
    forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (ell a)
xs (forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
passertSorted forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
ys) forall a b. (a -> b) -> a -> b
$ \Term s a
x Term s (ell a)
xs' ->
      forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (ell a)
ys (forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
passertSorted forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs) forall a b. (a -> b) -> a -> b
$ \Term s a
y Term s (ell a)
ys' ->
        (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
$ forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> (a :--> (ell a :--> ell a))))
-> Term s' a
-> Term s' (ell a)
-> Term s' a
-> Term s' (ell a)
-> Term s' (ell a)
go Term s (PComparator a)
cmp) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
ys'
  where
    go ::
      forall (s' :: S).
      Term s' (PComparator a) ->
      Term s' (a :--> ell a :--> a :--> ell a :--> ell a) ->
      Term s' a ->
      Term s' (ell a) ->
      Term s' a ->
      Term s' (ell a) ->
      Term s' (ell a)
    go :: forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (ell a :--> (a :--> (ell a :--> ell a))))
-> Term s' a
-> Term s' (ell a)
-> Term s' a
-> Term s' (ell a)
-> Term s' (ell a)
go Term s' (PComparator a)
cmp Term s' (a :--> (ell a :--> (a :--> (ell a :--> ell a))))
self Term s' a
x Term s' (ell a)
xs Term s' a
y Term s' (ell a)
ys =
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y)
        ( forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s' (ell a)
xs (forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (a :--> (ell a :--> ell a)))
passertSortedLookahead forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
ys) forall a b. (a -> b) -> a -> b
$ \Term s' a
x' Term s' (ell a)
xs' ->
            forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
              (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x')
              (Term s' (a :--> (ell a :--> (a :--> (ell a :--> ell a))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
ys)
              forall (a :: PType) (s :: S). Term s a
unorderedError
        )
        ( forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s' (ell a)
ys (forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (a :--> (ell a :--> ell a)))
passertSortedLookahead forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
xs) forall a b. (a -> b) -> a -> b
$ \Term s' a
y' Term s' (ell a)
ys' ->
            forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
              (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y')
              (Term s' (a :--> (ell a :--> (a :--> (ell a :--> ell a))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
xs forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
y' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (ell a)
ys')
              forall (a :: PType) (s :: S). Term s a
unorderedError
        )

{- | Sort a list-like by the 'POrd' instance of its contents.

 This uses a [merge sort](https://en.wikipedia.org/wiki/Merge_sort)
 implementation, which is also
 [stable](https://en.wikipedia.org/wiki/Sorting_algorithm#Stability). As this
 is a comparison sort, it requires a linearithmic ($n \log(n)$) number of
 comparisons to complete.

 @since 3.6.0
-}
psort ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (POrd a, PElemConstraint ell a, PListLike ell) =>
  Term s (ell a :--> ell a)
psort :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(POrd a, PElemConstraint ell a, PListLike ell) =>
Term s (ell a :--> ell a)
psort = 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 :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
psortBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#)

{- | As 'psort', but uses a custom 'PComparator' for order comparisons.

 @since 3.6.0
-}
psortBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> ell a)
psortBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
psortBy = 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 (PComparator a)
cmp Term s (ell a)
xs ->
    forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a)
-> (forall (s' :: S).
    Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a))
-> (forall (s' :: S).
    Term s' (PComparator a)
    -> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a))
-> Term s (PList (PList a) :--> ell a)
pmergeAllUnsafe
      Term s (PComparator a)
cmp
      (forall a b. a -> b -> a
const forall a. a -> a
id)
      forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s (PList a) -> Term s (PList a) -> Term s (PList a)
pmergeUnsafe
      #$ pmergeStart_2_3_4
      # cmp
      # xs

{- | As 'psort', but also throws out all duplicates according to the 'PEq'
 instance of its argument's contents.

 @since 3.6.0
-}
pnubSort ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (POrd a, PElemConstraint ell a, PListLike ell) =>
  Term s (ell a :--> ell a)
pnubSort :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(POrd a, PElemConstraint ell a, PListLike ell) =>
Term s (ell a :--> ell a)
pnubSort = 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 :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
pnubSortBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd @a forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#)

{- | As 'pnubSort', but uses a custom 'PComparator' for both ordering and
 equality.

 @since 3.6.0
-}
pnubSortBy ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> ell a)
pnubSortBy :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
pnubSortBy = 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 (PComparator a)
cmp Term s (ell a)
xs ->
    forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a)
-> (forall (s' :: S).
    Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a))
-> (forall (s' :: S).
    Term s' (PComparator a)
    -> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a))
-> Term s (PList (PList a) :--> ell a)
pmergeAllUnsafe
      Term s (PComparator a)
cmp
      (\Term s' (PComparator a)
cmp' Term s' (PList a)
xs' -> forall (a :: PType) (s :: S).
Term s (PComparator a :--> (PList a :--> PList a))
pnubUnsafe forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PList a)
xs')
      forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s (PList a) -> Term s (PList a) -> Term s (PList a)
pmergeUnsafeNoDupes
      #$ pmergeStart_2_3_4
      # cmp
      # xs

-- Helpers

-- Merges nested PLists and repacks into a list-like of your choice, given a
-- method of merging the PLists themselves.
--
-- Only call this if you are _sure_ that the 'inner' PLists are sorted according
-- to the argument comparator!
pmergeAllUnsafe ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a) ->
  (forall (s' :: S). Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a)) ->
  (forall (s' :: S). Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a)) ->
  Term s (PList (PList a) :--> ell a)
pmergeAllUnsafe :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a)
-> (forall (s' :: S).
    Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a))
-> (forall (s' :: S).
    Term s' (PComparator a)
    -> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a))
-> Term s (PList (PList a) :--> ell a)
pmergeAllUnsafe Term s (PComparator a)
cmp forall (s' :: S).
Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a)
whenSingleton forall (s' :: S).
Term s' (PComparator a)
-> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a)
whenMerging = 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 (PList (PList a))
xs -> forall (f :: PType -> PType) (g :: PType -> PType) (a :: PType)
       (s :: S).
(PIsListLike f a, PIsListLike g a) =>
Term s (f a :--> g a)
pconvertLists 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 :--> 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 Term s (PList (PList a) :--> PList a)
-> Term s (PList (PList a)) -> Term s (PList a)
go forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList (PList a))
xs
  where
    go ::
      Term s (PList (PList a) :--> PList a) ->
      Term s (PList (PList a)) ->
      Term s (PList a)
    go :: Term s (PList (PList a) :--> PList a)
-> Term s (PList (PList a)) -> Term s (PList a)
go Term s (PList (PList a) :--> PList a)
self Term s (PList (PList a))
xs = forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList (PList a))
xs forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (list a)
pnil forall a b. (a -> b) -> a -> b
$ \Term s (PList a)
y Term s (PList (PList a))
ys ->
      forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList (PList a))
ys (forall (s' :: S).
Term s' (PComparator a) -> Term s' (PList a) -> Term s' (PList a)
whenSingleton Term s (PComparator a)
cmp Term s (PList a)
y) forall a b. (a -> b) -> a -> b
$ \Term s (PList a)
_ Term s (PList (PList a))
_ ->
        Term s (PList (PList a) :--> PList a)
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (PList (PList a) :--> PList (PList a))
pmergePairs forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList (PList a))
xs
    pmergePairs :: Term s (PList (PList a) :--> PList (PList a))
    pmergePairs :: Term s (PList (PList a) :--> PList (PList a))
pmergePairs = 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 (PList (PList a) :--> PList (PList a))
self Term s (PList (PList a))
xs ->
      forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList (PList a))
xs forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (list a)
pnil forall a b. (a -> b) -> a -> b
$ \Term s (PList a)
x' Term s (PList (PList a))
xs' ->
        forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList (PList a))
xs' Term s (PList (PList a))
xs forall a b. (a -> b) -> a -> b
$ \Term s (PList a)
x'' Term s (PList (PList a))
xs'' ->
          forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (s' :: S).
Term s' (PComparator a)
-> Term s' (PList a) -> Term s' (PList a) -> Term s' (PList a)
whenMerging Term s (PComparator a)
cmp Term s (PList a)
x' Term s (PList a)
x'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (PList (PList a) :--> PList (PList a))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList (PList a))
xs''

-- Merges two PLists, leaving duplicates in place.
--
-- Only call this if you are _sure_ that the PLists are sorted according to the
-- argument comparator!
pmergeUnsafe ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s (PList a) ->
  Term s (PList a) ->
  Term s (PList a)
pmergeUnsafe :: forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s (PList a) -> Term s (PList a) -> Term s (PList a)
pmergeUnsafe Term s (PComparator a)
cmp Term s (PList a)
xs Term s (PList a)
ys =
  forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList a)
xs Term s (PList a)
ys forall a b. (a -> b) -> a -> b
$ \Term s a
x' Term s (PList a)
xs' ->
    (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 (a :--> (PList a :--> (PList a :--> PList a)))
-> Term s a
-> Term s (PList a)
-> Term s (PList a)
-> Term s (PList a)
go) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys
  where
    go ::
      Term s (a :--> PList a :--> PList a :--> PList a) ->
      Term s a ->
      Term s (PList a) ->
      Term s (PList a) ->
      Term s (PList a)
    go :: Term s (a :--> (PList a :--> (PList a :--> PList a)))
-> Term s a
-> Term s (PList a)
-> Term s (PList a)
-> Term s (PList a)
go Term s (a :--> (PList a :--> (PList a :--> PList a)))
self Term s a
x' Term s (PList a)
xs' Term s (PList a)
ys' =
      forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList a)
ys' (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs') forall a b. (a -> b) -> a -> b
$ \Term s a
y'' Term s (PList a)
ys'' ->
        forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
          (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'')
          (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (a :--> (PList a :--> (PList a :--> PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs')
          (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (a :--> (PList a :--> (PList a :--> PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys'')

-- Merges two PLists, throwing out duplicates.
--
-- Only call this if you are _sure_ that the PLists are sorted according to the
-- argument comparator!
pmergeUnsafeNoDupes ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s (PList a) ->
  Term s (PList a) ->
  Term s (PList a)
pmergeUnsafeNoDupes :: forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s (PList a) -> Term s (PList a) -> Term s (PList a)
pmergeUnsafeNoDupes Term s (PComparator a)
cmp Term s (PList a)
xs Term s (PList a)
ys =
  forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList a)
xs (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (PList a :--> PList a))
pnubUnsafe forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys) forall a b. (a -> b) -> a -> b
$ \Term s a
x' Term s (PList a)
xs' ->
    (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 (a :--> (PList a :--> (PList a :--> PList a)))
-> Term s a
-> Term s (PList a)
-> Term s (PList a)
-> Term s (PList a)
go) forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys
  where
    go ::
      Term s (a :--> PList a :--> PList a :--> PList a) ->
      Term s a ->
      Term s (PList a) ->
      Term s (PList a) ->
      Term s (PList a)
    go :: Term s (a :--> (PList a :--> (PList a :--> PList a)))
-> Term s a
-> Term s (PList a)
-> Term s (PList a)
-> Term s (PList a)
go Term s (a :--> (PList a :--> (PList a :--> PList a)))
self Term s a
x' Term s (PList a)
xs' Term s (PList a)
ys' =
      forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (PList a)
ys' (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (PList a :--> PList a))
pnubUnsafe forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp 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 (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs') forall a b. (a -> b) -> a -> b
$ \Term s a
y'' Term s (PList a)
ys'' ->
        forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> POrdering)))
pcompareBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'') forall a b. (a -> b) -> a -> b
$ \case
          POrdering s
PLT -> forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (a :--> (PList a :--> (PList a :--> PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs'
          POrdering s
PEQ -> Term s (a :--> (PList a :--> (PList a :--> PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs'
          POrdering s
PGT -> forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y'' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (a :--> (PList a :--> (PList a :--> PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
xs' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PList a)
ys''

-- Removes all duplicates from a sorted list.
--
-- Only call this if you are _sure_ that the PLists are sorted according to the
-- argument comparator!
pnubUnsafe ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a :--> PList a :--> PList a)
pnubUnsafe :: forall (a :: PType) (s :: S).
Term s (PComparator a :--> (PList a :--> PList a))
pnubUnsafe = 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 (PComparator a)
cmp ->
    forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
(Term s (a :--> (ell a :--> r))
 -> Term s a -> Term s a -> Term s (ell a) -> Term s r)
-> (Term s a -> Term s r) -> Term s r -> Term s (ell a :--> r)
precListLookahead (forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (PList a :--> PList a))
-> Term s' a
-> Term s' a
-> Term s' (PList a)
-> Term s' (PList a)
go Term s (PComparator a)
cmp) (forall (list :: PType -> PType) (a :: PType) (s :: S).
PIsListLike list a =>
Term s (a :--> list a)
psingleton 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)
pnil
  where
    go ::
      forall (s' :: S).
      Term s' (PComparator a) ->
      Term s' (a :--> PList a :--> PList a) ->
      Term s' a ->
      Term s' a ->
      Term s' (PList a) ->
      Term s' (PList a)
    go :: forall (s' :: S).
Term s' (PComparator a)
-> Term s' (a :--> (PList a :--> PList a))
-> Term s' a
-> Term s' a
-> Term s' (PList a)
-> Term s' (PList a)
go Term s' (PComparator a)
cmp Term s' (a :--> (PList a :--> PList a))
self Term s' a
h Term s' a
peek Term s' (PList a)
t =
      forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
        (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pequateBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek)
        (Term s' (a :--> (PList a :--> PList a))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PList a)
t)
        (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s' (a :--> (PList a :--> PList a))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' a
peek forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s' (PList a)
t)

-- Breaks the argument into sorted chunks; currently, these range in size from 2
-- to 4, with preference for larger chunks. To speed up the chunk sorting, we
-- use sorting networks.
pmergeStart_2_3_4 ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> PList (PList a))
pmergeStart_2_3_4 :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PList (PList a)))
pmergeStart_2_3_4 = forall (a :: PType) (s :: S).
HasCallStack =>
ClosedTerm a -> Term s a
phoistAcyclic forall a b. (a -> b) -> a -> b
$
  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 (PComparator a :--> (ell a :--> PList (PList a)))
self Term s (PComparator a)
cmp ->
    forall (r :: PType) (s' :: S).
Term s' r
-> (Term s' a -> Term s' (ell a) -> Term s' r)
-> Term s' (ell a)
-> Term s' r
pmatchList forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (list a)
pnil forall a b. (a -> b) -> a -> b
$
      \Term s a
_0 -> forall (r :: PType) (s' :: S).
Term s' r
-> (Term s' a -> Term s' (ell a) -> Term s' r)
-> Term s' (ell a)
-> Term s' r
pmatchList (forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [forall (a :: PType) (s :: S). Term s a -> Term s (PList a)
psing Term s a
_0]) forall a b. (a -> b) -> a -> b
$
        \Term s a
_1 -> forall (r :: PType) (s' :: S).
Term s' r
-> (Term s' a -> Term s' (ell a) -> Term s' r)
-> Term s' (ell a)
-> Term s' r
pmatchList (forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [forall (a :: PType) (s :: S).
Term s (PComparator a) -> Term s a -> Term s a -> Term s (PList a)
psort2 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1]) forall a b. (a -> b) -> a -> b
$
          \Term s a
_2 -> forall (r :: PType) (s' :: S).
Term s' r
-> (Term s' a -> Term s' (ell a) -> Term s' r)
-> Term s' (ell a)
-> Term s' r
pmatchList (forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s a -> Term s a -> Term s a -> Term s (PList a)
psort3 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 Term s a
_2]) forall a b. (a -> b) -> a -> b
$
            \Term s a
_3 Term s (ell a)
rest -> 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 -> Term s (PList a) -> PList a s
PSCons (forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s a -> Term s a -> Term s a -> Term s a -> Term s (PList a)
psort4 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 Term s a
_2 Term s a
_3) (Term s (PComparator a :--> (ell a :--> PList (PList a)))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
rest)
  where
    pmatchList ::
      forall (r :: S -> Type) (s' :: S).
      Term s' r ->
      (Term s' a -> Term s' (ell a) -> Term s' r) ->
      Term s' (ell a) ->
      Term s' r
    pmatchList :: forall (r :: PType) (s' :: S).
Term s' r
-> (Term s' a -> Term s' (ell a) -> Term s' r)
-> Term s' (ell a)
-> Term s' r
pmatchList = forall a b c. (a -> b -> c) -> b -> a -> c
flip forall (list :: PType -> PType) (a :: PType) (s :: S) (r :: PType).
(PListLike list, PElemConstraint list a) =>
(Term s a -> Term s (list a) -> Term s r)
-> Term s r -> Term s (list a) -> Term s r
pelimList

-- Unhoisted Foldable lifter
plist ::
  forall (a :: S -> Type) (f :: Type -> Type) (s :: S).
  (Foldable f) =>
  f (Term s a) ->
  Term s (PList a)
plist :: forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist = forall (t :: Type -> Type) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (\Term s a
x -> forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (a :: PType) (s :: S).
Term s a -> Term s (PList a) -> PList a s
PSCons Term s a
x) (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (a :: PType) (s :: S). PList a s
PSNil)

-- Unhoisted singleton maker
psing ::
  forall (a :: S -> Type) (s :: S).
  Term s a ->
  Term s (PList a)
psing :: forall (a :: PType) (s :: S). Term s a -> Term s (PList a)
psing Term s a
x = forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [Term s a
x]

-- Two-item sorting network; basically a comparison.
psort2 ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s a ->
  Term s a ->
  Term s (PList a)
psort2 :: forall (a :: PType) (s :: S).
Term s (PComparator a) -> Term s a -> Term s a -> Term s (PList a)
psort2 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 =
  forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 forall a b. (a -> b) -> a -> b
$ \Term s a
_0 Term s a
_1 ->
    forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [Term s a
_0, Term s a
_1]

-- Three-item sorting network. The layers are:
--

-- * Arg 0 vs arg 2

-- * Arg 0 vs arg 1

-- * Arg 1 vs arg 2
psort3 ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s a ->
  Term s a ->
  Term s a ->
  Term s (PList a)
psort3 :: forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s a -> Term s a -> Term s a -> Term s (PList a)
psort3 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 Term s a
_2 =
  forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_0 Term s a
_2 forall a b. (a -> b) -> a -> b
$ \Term s a
_0 Term s a
_2 ->
    forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 forall a b. (a -> b) -> a -> b
$ \Term s a
_0 Term s a
_1 ->
      forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_1 Term s a
_2 forall a b. (a -> b) -> a -> b
$ \Term s a
_1 Term s a
_2 ->
        forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [Term s a
_0, Term s a
_1, Term s a
_2]

-- Four-item sorting network. The layers are:
--

-- * Arg 0 vs arg 2 and arg 1 versus arg 3

-- * Arg 0 vs arg 1 and arg 2 versus arg 3

-- * Arg 1 versus arg 2

--
-- We perform the layer parallelism sequentially; it doesn't affect the
-- semantics, it just makes us sad because it's slow.
psort4 ::
  forall (a :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s a ->
  Term s a ->
  Term s a ->
  Term s a ->
  Term s (PList a)
psort4 :: forall (a :: PType) (s :: S).
Term s (PComparator a)
-> Term s a -> Term s a -> Term s a -> Term s a -> Term s (PList a)
psort4 Term s (PComparator a)
cmp Term s a
_0 Term s a
_1 Term s a
_2 Term s a
_3 =
  forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_0 Term s a
_2 forall a b. (a -> b) -> a -> b
$ \Term s a
_0 Term s a
_2 ->
    forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
_1 Term s a
_3 forall a b. (a -> b) -> a -> b
$ \Term s a
_1 Term s a
_3 ->
      (\Term
  s (PComparator a :--> (a :--> (a :--> (a :--> (a :--> PList a)))))
f -> Term
  s (PComparator a :--> (a :--> (a :--> (a :--> (a :--> PList a)))))
f forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
_0 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
_1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
_2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
_3) forall a b. (a -> b) -> a -> 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 a b. (a -> b) -> a -> b
$
            \Term s (PComparator a)
cmp' Term s a
_0 Term s a
_1 Term s a
_2 Term s a
_3 ->
              forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp' Term s a
_0 Term s a
_1 forall a b. (a -> b) -> a -> b
$ \Term s a
_0 Term s a
_1 ->
                forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp' Term s a
_2 Term s a
_3 forall a b. (a -> b) -> a -> b
$ \Term s a
_2 Term s a
_3 ->
                  forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp' Term s a
_1 Term s a
_2 forall a b. (a -> b) -> a -> b
$ \Term s a
_1 Term s a
_2 ->
                    forall (a :: PType) (f :: Type -> Type) (s :: S).
Foldable f =>
f (Term s a) -> Term s (PList a)
plist [Term s a
_0, Term s a
_1, Term s a
_2, Term s a
_3]

-- Runs a 'sorting network layer' driven by a given PComparator. Written in CPS
-- style for efficiency.
--
-- Note
--
-- Chaining this leads to duplication in each 'pif' branch, which can cause
-- script sizes to blow up. Specifically, the total number of 'pifs' will be
-- 2 ^ (n - 1), where n is the length of the 'swap chain'.
--
-- To reduce blowup, you want to 'cut' your swap chain into larger 'stages',
-- separated by a lambda. You can see an example of how to do this in the psort4
-- function in this module.
--
-- Three to four swaps per 'stage' is a good compromise. Using more lambdas
-- increases execution costs, as each lambda needs to be hoisted, or you still
-- end up with duplication costs. In order to hoist such a lambda, it needs to
-- receive the entire list as an argument, as otherwise, it would need access to
-- out-of-scope variables, which is prevented by the type system.
--
-- Using 'plet' instead of hoisting doesn't really help, unless the whole
-- sorting network is only called once, since you pay almost the same cost for
-- each execution as hoisting would require.
pswap ::
  forall (a :: S -> Type) (r :: S -> Type) (s :: S).
  Term s (PComparator a) ->
  Term s a ->
  Term s a ->
  (Term s a -> Term s a -> Term s r) ->
  Term s r
pswap :: forall (a :: PType) (r :: PType) (s :: S).
Term s (PComparator a)
-> Term s a
-> Term s a
-> (Term s a -> Term s a -> Term s r)
-> Term s r
pswap Term s (PComparator a)
cmp Term s a
x Term s a
y Term s a -> Term s a -> Term s r
cont = forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
y) (Term s a -> Term s a -> Term s r
cont Term s a
x Term s a
y) (Term s a -> Term s a -> Term s r
cont Term s a
y Term s a
x)

-- ensures the argument is sorted by the comparator, erroring if not
passertSorted ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> ell a :--> ell a)
passertSorted :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> ell a))
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 (PComparator a)
cmp Term s (ell a)
xs ->
    forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (ell a)
xs Term s (ell a)
xs forall a b. (a -> b) -> a -> b
$ \Term s a
x' Term s (ell a)
xs' ->
      forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (a :--> (ell a :--> ell a)))
passertSortedLookahead forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs'

-- as passertSorted, but with the 'lookahead' already done
passertSortedLookahead ::
  forall (a :: S -> Type) (ell :: (S -> Type) -> S -> Type) (s :: S).
  (PElemConstraint ell a, PListLike ell) =>
  Term s (PComparator a :--> a :--> ell a :--> ell a)
passertSortedLookahead :: forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (a :--> (ell a :--> ell a)))
passertSortedLookahead = 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 (PComparator a)
cmp Term s a
x Term s (ell a)
xs ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (forall (a :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (PComparator a :--> (ell a :--> PBool))
pisSortedBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs)
      ( forall (a :: PType) (r :: PType) (ell :: PType -> PType) (s :: S).
(PElemConstraint ell a, PListLike ell) =>
Term s (ell a)
-> Term s r -> (Term s a -> Term s (ell a) -> Term s r) -> Term s r
phandleList Term s (ell a)
xs (forall (list :: PType -> PType) (a :: PType) (s :: S).
PIsListLike list a =>
Term s (a :--> list a)
psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x) forall a b. (a -> b) -> a -> b
$ \Term s a
x' Term s (ell a)
_ ->
          forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
            (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pleqBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x')
            (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (ell a)
xs)
            forall (a :: PType) (s :: S). Term s a
unorderedError
      )
      forall (a :: PType) (s :: S). Term s a
unorderedError

unorderedError :: forall (a :: S -> Type) (s :: S). Term s a
unorderedError :: forall (a :: PType) (s :: S). Term s a
unorderedError = forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"ptryMergeBy: argument list-like out of order"

-- Helper for dragging a comparator through a map. We hide this away to ensure
-- that people actually use the comparator as intended.
pcmpMap ::
  forall (k :: S -> Type) (v :: S -> Type) (s :: S).
  (POrd k, PIsData k, PIsData v) =>
  Term
    s
    ( (PComparator v :--> v :--> v :--> PBool)
        :--> PComparator v
        :--> k
        :--> k
        :--> v
        :--> v
        :--> PMap 'Sorted k v
        :--> PMap 'Sorted k v
        :--> PBool
    ) ->
  Term s (PComparator v :--> v :--> v :--> PBool) ->
  Term s (PComparator v) ->
  Term s k ->
  Term s k ->
  Term s v ->
  Term s v ->
  Term s (PMap 'Sorted k v) ->
  Term s (PMap 'Sorted k v) ->
  Term s PBool
pcmpMap :: forall (k :: PType) (v :: PType) (s :: S).
(POrd k, PIsData k, PIsData v) =>
Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
-> Term s (PComparator v :--> (v :--> (v :--> PBool)))
-> Term s (PComparator v)
-> Term s k
-> Term s k
-> Term s v
-> Term s v
-> Term s (PMap 'Sorted k v)
-> Term s (PMap 'Sorted k v)
-> Term s PBool
pcmpMap Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
self Term s (PComparator v :--> (v :--> (v :--> PBool)))
doCmp Term s (PComparator v)
cmp Term s k
k1 Term s k
k2 Term s v
v1 Term s v
v2 Term s (PMap 'Sorted k v)
kv1 Term s (PMap 'Sorted k v)
kv2 = forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> POrdering)))
pcompareBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2) forall a b. (a -> b) -> a -> b
$ \case
  -- Need to advance the first key.
  POrdering s
PLT -> forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
kv1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1' Term s v
v1' Term s (PMap 'Sorted k v)
kv1' ->
    Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v :--> (v :--> (v :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2
  -- Keys are equal, check our comparison.
  POrdering s
PEQ ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (Term s (PComparator v :--> (v :--> (v :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2)
      ( forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
kv1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k1' Term s v
v1' Term s (PMap 'Sorted k v)
kv1' ->
          forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
kv2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2' Term s v
v2' Term s (PMap 'Sorted k v)
kv2' ->
            Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v :--> (v :--> (v :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2'
      )
      (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PFalse)
  -- Need to advance the second key.
  POrdering s
PGT -> forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term s (PMap 'Sorted k v)
kv2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s k
k2' Term s v
v2' Term s (PMap 'Sorted k v)
kv2' ->
    Term
  s
  ((PComparator v :--> (v :--> (v :--> PBool)))
   :--> (PComparator v
         :--> (k
               :--> (k
                     :--> (v
                           :--> (v
                                 :--> (PMap 'Sorted k v :--> (PMap 'Sorted k v :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v :--> (v :--> (v :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator v)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s k
k2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s v
v2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted k v)
kv2'

-- Helper for dragging a comparator through a PValue. We hide this away to
-- ensure that people actually use the comparator as intended.
pcmpValue ::
  forall (s :: S).
  Term
    s
    ( ( PComparator PInteger
          :--> PMap 'Sorted PTokenName PInteger
          :--> PMap 'Sorted PTokenName PInteger
          :--> PBool
      )
        :--> PComparator PInteger
        :--> PCurrencySymbol
        :--> PCurrencySymbol
        :--> PMap 'Sorted PTokenName PInteger
        :--> PMap 'Sorted PTokenName PInteger
        :--> PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
        :--> PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
        :--> PBool
    ) ->
  Term
    s
    ( PComparator PInteger
        :--> PMap 'Sorted PTokenName PInteger
        :--> PMap 'Sorted PTokenName PInteger
        :--> PBool
    ) ->
  Term s (PComparator PInteger) ->
  Term s PCurrencySymbol ->
  Term s PCurrencySymbol ->
  Term s (PMap 'Sorted PTokenName PInteger) ->
  Term s (PMap 'Sorted PTokenName PInteger) ->
  Term s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) ->
  Term s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)) ->
  Term s PBool
pcmpValue :: forall (s :: S).
Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
-> Term
     s
     (PComparator PInteger
      :--> (PMap 'Sorted PTokenName PInteger
            :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
-> Term s (PComparator PInteger)
-> Term s PCurrencySymbol
-> Term s PCurrencySymbol
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term s (PMap 'Sorted PTokenName PInteger)
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term
     s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
-> Term s PBool
pcmpValue Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
self Term
  s
  (PComparator PInteger
   :--> (PMap 'Sorted PTokenName PInteger
         :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
doCmp Term s (PComparator PInteger)
cmp Term s PCurrencySymbol
k1 Term s PCurrencySymbol
k2 Term s (PMap 'Sorted PTokenName PInteger)
v1 Term s (PMap 'Sorted PTokenName PInteger)
v2 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 = forall (a :: PType) (s :: S) (b :: PType).
PlutusType a =>
Term s a -> (a s -> Term s b) -> Term s b
pmatch (forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> POrdering)))
pcompareBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# forall (a :: PType) (s :: S). POrd a => Term s (PComparator a)
pfromOrd forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2) forall a b. (a -> b) -> a -> b
$ \case
  -- Need to advance the first key.
  POrdering s
PLT -> forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1' Term s (PMap 'Sorted PTokenName PInteger)
v1' Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1' ->
    Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PComparator PInteger
   :--> (PMap 'Sorted PTokenName PInteger
         :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2
  -- Key are equal, do an inner map comparison using comparator.
  POrdering s
PEQ ->
    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
      (Term
  s
  (PComparator PInteger
   :--> (PMap 'Sorted PTokenName PInteger
         :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2)
      ( forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k1' Term s (PMap 'Sorted PTokenName PInteger)
v1' Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1' ->
          forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2' Term s (PMap 'Sorted PTokenName PInteger)
v2' Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2' ->
            Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PComparator PInteger
   :--> (PMap 'Sorted PTokenName PInteger
         :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2'
      )
      (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PFalse)
  -- Need to advance the second key.
  POrdering s
PGT -> forall (r :: PType) (k :: PType) (v :: PType) (s :: S).
(PIsData k, PIsData v) =>
Term s (PMap 'Sorted k v)
-> Term s r
-> (Term s k -> Term s v -> Term s (PMap 'Sorted k v) -> Term s r)
-> Term s r
phandleMin Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2 (forall (a :: PType) (s :: S). PlutusType a => a s -> Term s a
pcon forall (s :: S). PBool s
PTrue) forall a b. (a -> b) -> a -> b
$ \Term s PCurrencySymbol
k2' Term s (PMap 'Sorted PTokenName PInteger)
v2' Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2' ->
    Term
  s
  ((PComparator PInteger
    :--> (PMap 'Sorted PTokenName PInteger
          :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
   :--> (PComparator PInteger
         :--> (PCurrencySymbol
               :--> (PCurrencySymbol
                     :--> (PMap 'Sorted PTokenName PInteger
                           :--> (PMap 'Sorted PTokenName PInteger
                                 :--> (PMap
                                         'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger)
                                       :--> (PMap
                                               'Sorted
                                               PCurrencySymbol
                                               (PMap 'Sorted PTokenName PInteger)
                                             :--> PBool))))))))
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s
  (PComparator PInteger
   :--> (PMap 'Sorted PTokenName PInteger
         :--> (PMap 'Sorted PTokenName PInteger :--> PBool)))
doCmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator PInteger)
cmp forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s PCurrencySymbol
k2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PMap 'Sorted PTokenName PInteger)
v2' forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv1 forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term
  s (PMap 'Sorted PCurrencySymbol (PMap 'Sorted PTokenName PInteger))
kv2'

{- | Insert a value to a list with no duplicate, assuming the list is sorted
     in ascending order. Error out if the value is already in the list.

     @since 3.14.1
-}
pinsertUniqueBy ::
  forall (list :: PType -> PType) (a :: PType) (s :: S).
  (PIsListLike list a) =>
  Term s (PComparator a :--> a :--> list a :--> list a)
pinsertUniqueBy :: forall (list :: PType -> PType) (a :: PType) (s :: S).
PIsListLike list a =>
Term s (PComparator a :--> (a :--> (list a :--> list a)))
pinsertUniqueBy = 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 (PComparator a)
c Term s a
x ->
    let lt :: Term s (a :--> (a :--> PBool))
lt = forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
plessThanBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
c
        eq :: Term s (a :--> (a :--> PBool))
eq = forall (a :: PType) (s :: S).
Term s (PComparator a :--> (a :--> (a :--> PBool)))
pequateBy forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (PComparator a)
c
     in forall (list :: PType -> PType) (a :: PType) (s :: S) (r :: PType).
PIsListLike list a =>
(Term s (list a :--> r) -> Term s a -> Term s (list a) -> Term s r)
-> (Term s (list a :--> r) -> Term s r) -> Term s (list a :--> r)
precList
          ( \Term s (list a :--> list a)
self Term s a
h Term s (list a)
t ->
              let ensureUniqueness :: Term s (list a) -> Term s (list a)
ensureUniqueness =
                    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
                      (Term s (a :--> (a :--> PBool))
eq forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
h)
                      (forall (s :: S) (a :: PType). Term s PString -> Term s a
ptraceError Term s PString
"inserted value already exists")
                  next :: Term s (list a)
next =
                    forall (s :: S) (a :: PType).
Term s PBool -> Term s a -> Term s a -> Term s a
pif
                      (Term s (a :--> (a :--> PBool))
lt forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
h)
                      (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x 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 (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (list a)
t)
                      (forall (list :: PType -> PType) (a :: PType) (s :: S).
(PListLike list, PElemConstraint list a) =>
Term s (a :--> (list a :--> list a))
pcons forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
h forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
#$ Term s (list a :--> list a)
self forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s (list a)
t)
               in Term s (list a) -> Term s (list a)
ensureUniqueness Term s (list a)
next
          )
          (forall a b. a -> b -> a
const forall a b. (a -> b) -> a -> b
$ forall (list :: PType -> PType) (a :: PType) (s :: S).
PIsListLike list a =>
Term s (a :--> list a)
psingleton forall (s :: S) (a :: PType) (b :: PType).
HasCallStack =>
Term s (a :--> b) -> Term s a -> Term s b
# Term s a
x)