{-# 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 = BuiltinInteger -> BuiltinInteger
forall a. a -> a
id
instance ToBuiltin Integer BuiltinInteger where
{-# INLINABLE toBuiltin #-}
toBuiltin :: BuiltinInteger -> BuiltinInteger
toBuiltin = BuiltinInteger -> BuiltinInteger
forall a. a -> a
id
instance FromBuiltin BuiltinBool Bool where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinBool -> Bool
fromBuiltin BuiltinBool
b = BuiltinBool -> Bool -> Bool -> Bool
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 = BuiltinUnit -> () -> ()
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 = (String -> BuiltinString) -> String -> BuiltinString
forall a. a -> a
Magic.noinline String -> BuiltinString
stringToBuiltinString
{-# INLINABLE stringToBuiltinString #-}
stringToBuiltinString :: Haskell.String -> BuiltinString
stringToBuiltinString :: String -> BuiltinString
stringToBuiltinString String
str = BuiltinString -> BuiltinString
forall a. a -> a
obfuscatedId (Text -> BuiltinString
BuiltinString (Text -> BuiltinString) -> Text -> BuiltinString
forall a b. (a -> b) -> a -> b
$ String -> Text
pack String
str)
{-# NOINLINE obfuscatedId #-}
obfuscatedId :: a -> a
obfuscatedId :: 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 = (String -> BuiltinByteString) -> String -> BuiltinByteString
forall a. a -> a
Magic.noinline String -> BuiltinByteString
stringToBuiltinByteString
{-# INLINABLE stringToBuiltinByteString #-}
stringToBuiltinByteString :: Haskell.String -> BuiltinByteString
stringToBuiltinByteString :: String -> BuiltinByteString
stringToBuiltinByteString String
str = BuiltinString -> BuiltinByteString
encodeUtf8 (BuiltinString -> BuiltinByteString)
-> BuiltinString -> BuiltinByteString
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 = (arep -> a
forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin (arep -> a) -> arep -> a
forall a b. (a -> b) -> a -> b
$ BuiltinPair arep brep -> arep
forall a b. BuiltinPair a b -> a
fst BuiltinPair arep brep
p, brep -> b
forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin (brep -> b) -> brep -> b
forall a b. (a -> b) -> a -> b
$ BuiltinPair arep brep -> brep
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 = BuiltinList arep
-> (BuiltinUnit -> [a])
-> (BuiltinUnit -> [a])
-> BuiltinUnit
-> [a]
forall a b. BuiltinList a -> b -> b -> b
chooseList BuiltinList arep
l ([a] -> BuiltinUnit -> [a]
forall a b. a -> b -> a
const []) (\BuiltinUnit
_ -> arep -> a
forall arep a. FromBuiltin arep a => arep -> a
fromBuiltin (BuiltinList arep -> arep
forall a. BuiltinList a -> a
head BuiltinList arep
l)a -> [a] -> [a]
forall a. a -> [a] -> [a]
:BuiltinList arep -> [a]
go (BuiltinList arep -> BuiltinList arep
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) = BuiltinData -> BuiltinList BuiltinData -> BuiltinList BuiltinData
forall a. a -> BuiltinList a -> BuiltinList a
mkCons BuiltinData
d ([BuiltinData] -> BuiltinList BuiltinData
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) = BuiltinPair BuiltinData BuiltinData
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a. a -> BuiltinList a -> BuiltinList a
mkCons ((BuiltinData, BuiltinData) -> BuiltinPair BuiltinData BuiltinData
forall a arep. ToBuiltin a arep => a -> arep
toBuiltin (BuiltinData, BuiltinData)
d) ([(BuiltinData, BuiltinData)]
-> BuiltinList (BuiltinPair BuiltinData BuiltinData)
forall a arep. ToBuiltin a arep => a -> arep
toBuiltin [(BuiltinData, BuiltinData)]
ds)
instance FromBuiltin BuiltinData BuiltinData where
{-# INLINABLE fromBuiltin #-}
fromBuiltin :: BuiltinData -> BuiltinData
fromBuiltin = BuiltinData -> BuiltinData
forall a. a -> a
id
instance ToBuiltin BuiltinData BuiltinData where
{-# INLINABLE toBuiltin #-}
toBuiltin :: BuiltinData -> BuiltinData
toBuiltin = BuiltinData -> BuiltinData
forall a. a -> a
id