{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
module PlutusTx.Builtins.Class where
import Data.ByteString (ByteString)
import PlutusTx.Builtins.Internal
import Data.String (IsString (..))
import Data.Text (Text, pack)
import GHC.Magic qualified as Magic
import PlutusTx.Base (const, id, ($))
import PlutusTx.Bool (Bool (..))
import PlutusTx.Integer (Integer)
import Prelude qualified as Haskell (String)
class FromBuiltin arep a | arep -> a where
fromBuiltin :: arep -> a
class ToBuiltin a arep | a -> arep where
toBuiltin :: a -> arep
instance FromBuiltin BuiltinInteger Integer where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinInteger -> BuiltinInteger
fromBuiltin = forall a. a -> a
id
instance ToBuiltin Integer BuiltinInteger where
{-# INLINABLE toBuiltin #-}
toBuiltin :: BuiltinInteger -> BuiltinInteger
toBuiltin = forall a. a -> a
id
instance FromBuiltin BuiltinBool Bool where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinBool -> Bool
fromBuiltin BuiltinBool
b = forall a. BuiltinBool -> a -> a -> a
ifThenElse BuiltinBool
b Bool
True Bool
False
instance ToBuiltin Bool BuiltinBool where
{-# INLINABLE toBuiltin #-}
toBuiltin :: Bool -> BuiltinBool
toBuiltin Bool
b = if Bool
b then BuiltinBool
true else BuiltinBool
false
instance FromBuiltin BuiltinUnit () where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinUnit -> ()
fromBuiltin BuiltinUnit
u = forall a. BuiltinUnit -> a -> a
chooseUnit BuiltinUnit
u ()
instance ToBuiltin () BuiltinUnit where
{-# INLINABLE toBuiltin #-}
toBuiltin :: () -> BuiltinUnit
toBuiltin ()
x = case ()
x of () -> BuiltinUnit
unitval
instance FromBuiltin BuiltinByteString ByteString where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinByteString -> ByteString
fromBuiltin (BuiltinByteString ByteString
b) = ByteString
b
instance ToBuiltin ByteString BuiltinByteString where
{-# INLINABLE toBuiltin #-}
toBuiltin :: ByteString -> BuiltinByteString
toBuiltin = ByteString -> BuiltinByteString
BuiltinByteString
instance IsString BuiltinString where
{-# INLINE fromString #-}
fromString :: String -> BuiltinString
fromString = forall a. a -> a
Magic.noinline String -> BuiltinString
stringToBuiltinString
{-# INLINABLE stringToBuiltinString #-}
stringToBuiltinString :: Haskell.String -> BuiltinString
stringToBuiltinString :: String -> BuiltinString
stringToBuiltinString String
str = forall a. a -> a
obfuscatedId (Text -> BuiltinString
BuiltinString forall a b. (a -> b) -> a -> b
$ String -> Text
pack String
str)
{-# NOINLINE obfuscatedId #-}
obfuscatedId :: a -> a
obfuscatedId :: forall a. a -> a
obfuscatedId a
a = a
a
instance FromBuiltin BuiltinString Text where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinString -> Text
fromBuiltin (BuiltinString Text
t) = Text
t
instance ToBuiltin Text BuiltinString where
{-# INLINABLE toBuiltin #-}
toBuiltin :: Text -> BuiltinString
toBuiltin = Text -> BuiltinString
BuiltinString
instance IsString BuiltinByteString where
{-# INLINE fromString #-}
fromString :: String -> BuiltinByteString
fromString = forall a. a -> a
Magic.noinline String -> BuiltinByteString
stringToBuiltinByteString
{-# INLINABLE stringToBuiltinByteString #-}
stringToBuiltinByteString :: Haskell.String -> BuiltinByteString
stringToBuiltinByteString :: String -> BuiltinByteString
stringToBuiltinByteString String
str = BuiltinString -> BuiltinByteString
encodeUtf8 forall a b. (a -> b) -> a -> b
$ String -> BuiltinString
stringToBuiltinString String
str
instance (FromBuiltin arep a, FromBuiltin brep b) => FromBuiltin (BuiltinPair arep brep) (a,b) where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinPair arep brep -> (a, b)
fromBuiltin BuiltinPair arep brep
p = (forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin forall a b. (a -> b) -> a -> b
$ forall a b. BuiltinPair a b -> a
fst BuiltinPair arep brep
p, forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin forall a b. (a -> b) -> a -> b
$ forall a b. BuiltinPair a b -> b
snd BuiltinPair arep brep
p)
instance ToBuiltin (BuiltinData, BuiltinData) (BuiltinPair BuiltinData BuiltinData) where
{-# INLINABLE toBuiltin #-}
toBuiltin :: (BuiltinData, BuiltinData) -> BuiltinPair BuiltinData BuiltinData
toBuiltin (BuiltinData
d1, BuiltinData
d2) = BuiltinData -> BuiltinData -> BuiltinPair BuiltinData BuiltinData
mkPairData BuiltinData
d1 BuiltinData
d2
instance FromBuiltin arep a => FromBuiltin (BuiltinList arep) [a] where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinList arep -> [a]
fromBuiltin = BuiltinList arep -> [a]
go
where
{-# INLINABLE go #-}
go :: BuiltinList arep -> [a]
go :: BuiltinList arep -> [a]
go BuiltinList arep
l = forall a b. BuiltinList a -> b -> b -> b
chooseList BuiltinList arep
l (forall a b. a -> b -> a
const []) (\BuiltinUnit
_ -> forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin (forall a. BuiltinList a -> a
head BuiltinList arep
l)forall a. a -> [a] -> [a]
:BuiltinList arep -> [a]
go (forall a. BuiltinList a -> BuiltinList a
tail BuiltinList arep
l)) BuiltinUnit
unitval
instance ToBuiltin [BuiltinData] (BuiltinList BuiltinData) where
{-# INLINABLE toBuiltin #-}
toBuiltin :: [BuiltinData] -> BuiltinList BuiltinData
toBuiltin [] = BuiltinUnit -> BuiltinList BuiltinData
mkNilData BuiltinUnit
unitval
toBuiltin (BuiltinData
d:[BuiltinData]
ds) = forall a. a -> BuiltinList a -> BuiltinList a
mkCons BuiltinData
d (forall a arep. ToBuiltin a arep => a -> arep
toBuiltin [BuiltinData]
ds)
instance ToBuiltin [(BuiltinData, BuiltinData)] (BuiltinList (BuiltinPair BuiltinData BuiltinData)) where
{-# INLINABLE toBuiltin #-}
toBuiltin :: [(BuiltinData, BuiltinData)]
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
toBuiltin [] = BuiltinUnit -> BuiltinList (BuiltinPair BuiltinData BuiltinData)
mkNilPairData BuiltinUnit
unitval
toBuiltin ((BuiltinData, BuiltinData)
d:[(BuiltinData, BuiltinData)]
ds) = forall a. a -> BuiltinList a -> BuiltinList a
mkCons (forall a arep. ToBuiltin a arep => a -> arep
toBuiltin (BuiltinData, BuiltinData)
d) (forall a arep. ToBuiltin a arep => a -> arep
toBuiltin [(BuiltinData, BuiltinData)]
ds)
instance FromBuiltin BuiltinData BuiltinData where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinData -> BuiltinData
fromBuiltin = forall a. a -> a
id
instance ToBuiltin BuiltinData BuiltinData where
{-# INLINABLE toBuiltin #-}
toBuiltin :: BuiltinData -> BuiltinData
toBuiltin = forall a. a -> a
id