-- | Plutus Tx basic functions.
module PlutusTx.Base (fst, snd, curry, uncurry, ($), flip, until, (.), const, id) where

import PlutusTx.Bool

{-# INLINABLE fst #-}
-- | Plutus Tx version of 'Data.Tuple.fst'
fst :: (a, b) -> a
fst :: forall a b. (a, b) -> a
fst (a
a, b
_) = a
a

{-# INLINABLE snd #-}
-- | Plutus Tx version of 'Data.Tuple.snd'
snd :: (a, b) -> b
snd :: forall a b. (a, b) -> b
snd (a
_, b
b) = b
b

{-# INLINABLE curry #-}
curry :: ((a, b) -> c) -> a -> b -> c
curry :: forall a b c. ((a, b) -> c) -> a -> b -> c
curry (a, b) -> c
f a
a b
b = (a, b) -> c
f (a
a, b
b)

{-# INLINABLE uncurry #-}
uncurry :: (a -> b -> c) -> (a, b) -> c
uncurry :: forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry a -> b -> c
f (a
a, b
b) = a -> b -> c
f a
a b
b

infixr 0 $
-- Normal $ is levity-polymorphic, which we can't handle.
{-# INLINABLE ($) #-}
-- | Plutus Tx version of 'Data.Function.($)'.
($) :: (a -> b) -> a -> b
a -> b
f $ :: forall a b. (a -> b) -> a -> b
$ a
a = a -> b
f a
a

{-# INLINABLE flip #-}
-- | Plutus Tx version of 'Prelude.flip'.
flip                    :: (a -> b -> c) -> b -> a -> c
flip :: forall a b c. (a -> b -> c) -> b -> a -> c
flip a -> b -> c
f b
x a
y              =  a -> b -> c
f a
y b
x

{-# INLINABLE until #-}
-- | Plutus Tx version of 'Prelude.until'.
until                   :: (a -> Bool) -> (a -> a) -> a -> a
until :: forall a. (a -> Bool) -> (a -> a) -> a -> a
until a -> Bool
p a -> a
f = a -> a
go
  where
    go :: a -> a
go a
x | a -> Bool
p a
x          = a
x
         | Bool
otherwise    = a -> a
go (a -> a
f a
x)

infixr 9 .
{-# INLINABLE (.) #-}
-- | Plutus Tx version of 'Prelude.(.)'.
(.)    :: (b -> c) -> (a -> b) -> a -> c
. :: forall b c a. (b -> c) -> (a -> b) -> a -> c
(.) b -> c
f a -> b
g = \a
x -> b -> c
f (a -> b
g a
x)


{-# INLINABLE const #-}
-- | Plutus Tx version of 'Prelude.const'.
const :: a -> b -> a
const :: forall a b. a -> b -> a
const a
x b
_ =  a
x

{-# INLINABLE id #-}
-- | Plutus Tx version of 'Prelude.id'.
id :: a -> a
id :: forall a. a -> a
id a
x = a
x