{-# LANGUAGE CPP #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -fno-specialise #-}
{-# OPTIONS_GHC -fno-omit-interface-pragmas #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module PlutusTx.Builtins.HasOpaque where
import PlutusTx.Base (id, ($))
import PlutusTx.Bool (Bool (..))
import PlutusTx.Builtins.Internal
import Data.Kind qualified as GHC
import Data.String (IsString (..))
import Data.Text qualified as Text
import Prelude qualified as Haskell (String)
#if MIN_VERSION_base(4,20,0)
import Prelude (type (~))
#endif
stringToBuiltinByteString :: Haskell.String -> BuiltinByteString
stringToBuiltinByteString :: String -> BuiltinByteString
stringToBuiltinByteString String
str = ByteString -> BuiltinByteString
BuiltinByteString (String -> ByteString
forall a. IsString a => String -> a
fromString String
str)
{-# OPAQUE stringToBuiltinByteString #-}
stringToBuiltinString :: Haskell.String -> BuiltinString
stringToBuiltinString :: String -> BuiltinString
stringToBuiltinString String
str = Text -> BuiltinString
BuiltinString (String -> Text
Text.pack String
str)
{-# OPAQUE stringToBuiltinString #-}
instance IsString BuiltinByteString where
fromString :: String -> BuiltinByteString
fromString = String -> BuiltinByteString
stringToBuiltinByteString
{-# INLINE fromString #-}
instance IsString BuiltinString where
fromString :: String -> BuiltinString
fromString = String -> BuiltinString
stringToBuiltinString
{-# INLINE fromString #-}
type HasToOpaque :: GHC.Type -> GHC.Type -> GHC.Constraint
class HasToOpaque a arep | a -> arep where
toOpaque :: a -> arep
default toOpaque :: a ~ arep => a -> arep
toOpaque = a -> a
a -> arep
forall a. a -> a
id
{-# INLINABLE toOpaque #-}
type HasFromOpaque :: GHC.Type -> GHC.Type -> GHC.Constraint
class HasFromOpaque arep a | arep -> a where
fromOpaque :: arep -> a
default fromOpaque :: a ~ arep => arep -> a
fromOpaque = arep -> arep
arep -> a
forall a. a -> a
id
{-# INLINABLE fromOpaque #-}
instance HasToOpaque BuiltinInteger BuiltinInteger
instance HasFromOpaque BuiltinInteger BuiltinInteger
instance HasToOpaque BuiltinByteString BuiltinByteString
instance HasFromOpaque BuiltinByteString BuiltinByteString
instance HasToOpaque BuiltinString BuiltinString
instance HasFromOpaque BuiltinString BuiltinString
instance HasToOpaque () BuiltinUnit where
toOpaque :: () -> BuiltinUnit
toOpaque ()
x = case ()
x of () -> BuiltinUnit
unitval
{-# INLINABLE toOpaque #-}
instance HasFromOpaque BuiltinUnit () where
fromOpaque :: BuiltinUnit -> ()
fromOpaque BuiltinUnit
u = BuiltinUnit -> () -> ()
forall a. BuiltinUnit -> a -> a
chooseUnit BuiltinUnit
u ()
{-# INLINABLE fromOpaque #-}
instance HasToOpaque Bool BuiltinBool where
toOpaque :: Bool -> BuiltinBool
toOpaque Bool
b = if Bool
b then BuiltinBool
true else BuiltinBool
false
{-# INLINABLE toOpaque #-}
instance HasFromOpaque BuiltinBool Bool where
fromOpaque :: BuiltinBool -> Bool
fromOpaque BuiltinBool
b = BuiltinBool -> Bool -> Bool -> Bool
forall a. BuiltinBool -> a -> a -> a
ifThenElse BuiltinBool
b Bool
True Bool
False
{-# INLINABLE fromOpaque #-}
mkNilOpaque :: BuiltinList a
mkNilOpaque :: forall a. BuiltinList a
mkNilOpaque = [a] -> BuiltinList a
forall a. [a] -> BuiltinList a
BuiltinList []
{-# OPAQUE mkNilOpaque #-}
class MkNil arep where
mkNil :: BuiltinList arep
mkNil = BuiltinList arep
forall a. BuiltinList a
mkNilOpaque
{-# INLINABLE mkNil #-}
instance MkNil BuiltinInteger
instance MkNil BuiltinBool
instance MkNil BuiltinData
instance MkNil (BuiltinPair BuiltinData BuiltinData)
instance (HasToOpaque a arep, MkNil arep) => HasToOpaque [a] (BuiltinList arep) where
toOpaque :: [a] -> BuiltinList arep
toOpaque = [a] -> BuiltinList arep
goList where
goList :: [a] -> BuiltinList arep
goList :: [a] -> BuiltinList arep
goList [] = BuiltinList arep
forall arep. MkNil arep => BuiltinList arep
mkNil
goList (a
d:[a]
ds) = arep -> BuiltinList arep -> BuiltinList arep
forall a. a -> BuiltinList a -> BuiltinList a
mkCons (a -> arep
forall a arep. HasToOpaque a arep => a -> arep
toOpaque a
d) ([a] -> BuiltinList arep
goList [a]
ds)
{-# INLINABLE toOpaque #-}
instance HasFromOpaque arep a => HasFromOpaque (BuiltinList arep) [a] where
fromOpaque :: BuiltinList arep -> [a]
fromOpaque = BuiltinList arep -> [a]
go
where
go :: BuiltinList arep -> [a]
go :: BuiltinList arep -> [a]
go = [a] -> (arep -> BuiltinList arep -> [a]) -> BuiltinList arep -> [a]
forall a r. r -> (a -> BuiltinList a -> r) -> BuiltinList a -> r
caseList' [] (\arep
x BuiltinList arep
xs -> arep -> a
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque arep
x a -> [a] -> [a]
forall a. a -> [a] -> [a]
: BuiltinList arep -> [a]
go BuiltinList arep
xs)
{-# INLINABLE go #-}
{-# INLINABLE fromOpaque #-}
instance HasToOpaque (BuiltinData, BuiltinData) (BuiltinPair BuiltinData BuiltinData) where
toOpaque :: (BuiltinData, BuiltinData) -> BuiltinPair BuiltinData BuiltinData
toOpaque (BuiltinData
d1, BuiltinData
d2) = BuiltinData -> BuiltinData -> BuiltinPair BuiltinData BuiltinData
mkPairData (BuiltinData -> BuiltinData
forall a arep. HasToOpaque a arep => a -> arep
toOpaque BuiltinData
d1) (BuiltinData -> BuiltinData
forall a arep. HasToOpaque a arep => a -> arep
toOpaque BuiltinData
d2)
{-# INLINABLE toOpaque #-}
instance (HasFromOpaque arep a, HasFromOpaque brep b) =>
HasFromOpaque (BuiltinPair arep brep) (a, b) where
fromOpaque :: BuiltinPair arep brep -> (a, b)
fromOpaque BuiltinPair arep brep
p = (arep -> a
forall arep a. HasFromOpaque arep a => arep -> a
fromOpaque (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. HasFromOpaque arep a => arep -> a
fromOpaque (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)
{-# INLINABLE fromOpaque #-}
instance HasToOpaque BuiltinData BuiltinData
instance HasFromOpaque BuiltinData BuiltinData
instance HasToOpaque BuiltinBLS12_381_G1_Element BuiltinBLS12_381_G1_Element
instance HasFromOpaque BuiltinBLS12_381_G1_Element BuiltinBLS12_381_G1_Element
instance HasToOpaque BuiltinBLS12_381_G2_Element BuiltinBLS12_381_G2_Element
instance HasFromOpaque BuiltinBLS12_381_G2_Element BuiltinBLS12_381_G2_Element
instance HasToOpaque BuiltinBLS12_381_MlResult BuiltinBLS12_381_MlResult
instance HasFromOpaque BuiltinBLS12_381_MlResult BuiltinBLS12_381_MlResult