{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module PlutusCore.Builtin.Polymorphism
( Opaque (..)
, SomeConstant (..)
, TyNameRep (..)
, TyVarRep
, TyAppRep
, TyForallRep
) where
import PlutusCore.Builtin.HasConstant
import PlutusCore.Core
import PlutusCore.Pretty
import Control.Applicative
import Control.Monad
import Control.Monad.IO.Class
import Data.Bifoldable
import Data.Bifunctor
import Data.Bitraversable
import Data.Kind qualified as GHC (Type)
import GHC.Ix
import GHC.TypeLits
import Universe
newtype Opaque val (rep :: GHC.Type) = Opaque
{ Opaque val rep -> val
unOpaque :: val
} deriving newtype ([Opaque val rep] -> Doc ann
Opaque val rep -> Doc ann
(forall ann. Opaque val rep -> Doc ann)
-> (forall ann. [Opaque val rep] -> Doc ann)
-> Pretty (Opaque val rep)
forall ann. [Opaque val rep] -> Doc ann
forall ann. Opaque val rep -> Doc ann
forall a.
(forall ann. a -> Doc ann)
-> (forall ann. [a] -> Doc ann) -> Pretty a
forall val rep ann. Pretty val => [Opaque val rep] -> Doc ann
forall val rep ann. Pretty val => Opaque val rep -> Doc ann
prettyList :: [Opaque val rep] -> Doc ann
$cprettyList :: forall val rep ann. Pretty val => [Opaque val rep] -> Doc ann
pretty :: Opaque val rep -> Doc ann
$cpretty :: forall val rep ann. Pretty val => Opaque val rep -> Doc ann
Pretty, Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep))))
Some (ValueOf (UniOf (Opaque val rep))) -> Opaque val rep
(forall err cause.
AsUnliftingError err =>
Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep)))))
-> (Some (ValueOf (UniOf (Opaque val rep))) -> Opaque val rep)
-> HasConstant (Opaque val rep)
forall term.
(forall err cause.
AsUnliftingError err =>
Maybe cause
-> term
-> Either (ErrorWithCause err cause) (Some (ValueOf (UniOf term))))
-> (Some (ValueOf (UniOf term)) -> term) -> HasConstant term
forall err cause.
AsUnliftingError err =>
Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep))))
forall val rep.
HasConstant val =>
Some (ValueOf (UniOf (Opaque val rep))) -> Opaque val rep
forall val rep err cause.
(HasConstant val, AsUnliftingError err) =>
Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep))))
fromConstant :: Some (ValueOf (UniOf (Opaque val rep))) -> Opaque val rep
$cfromConstant :: forall val rep.
HasConstant val =>
Some (ValueOf (UniOf (Opaque val rep))) -> Opaque val rep
asConstant :: Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep))))
$casConstant :: forall val rep err cause.
(HasConstant val, AsUnliftingError err) =>
Maybe cause
-> Opaque val rep
-> Either
(ErrorWithCause err cause)
(Some (ValueOf (UniOf (Opaque val rep))))
HasConstant)
type instance UniOf (Opaque val rep) = UniOf val
newtype SomeConstant uni (rep :: GHC.Type) = SomeConstant
{ SomeConstant uni rep -> Some (ValueOf uni)
unSomeConstant :: Some (ValueOf uni)
}
data TyNameRep (kind :: GHC.Type) = TyNameRep Symbol Nat
data family TyVarRep (name :: TyNameRep kind) :: kind
data family TyAppRep (fun :: dom -> cod) (arg :: dom) :: cod
data family TyForallRep (name :: TyNameRep kind) (a :: GHC.Type) :: GHC.Type
underTypeError :: void
underTypeError :: void
underTypeError = [Char] -> void
forall a. HasCallStack => [Char] -> a
error [Char]
"Panic: a 'TypeError' was bypassed"
type NoStandalonePolymorphicDataErrMsg =
'Text "Plutus type variables can't directly appear inside built-in types" ':$$:
'Text "Are you trying to define a polymorphic built-in function over a polymorphic type?" ':$$:
'Text "In that case you need to wrap all polymorphic built-in types having type variables" ':<>:
'Text " in them with either ‘SomeConstant’ or ‘Opaque’ depending on whether its the type" ':<>:
'Text " of an argument or the type of the result, respectively"
instance TypeError NoStandalonePolymorphicDataErrMsg => uni `Contains` TyVarRep where
knownUni :: uni (Esc TyVarRep)
knownUni = uni (Esc TyVarRep)
forall void. void
underTypeError
type NoConstraintsErrMsg =
'Text "Built-in functions are not allowed to have constraints" ':$$:
'Text "To fix this error instantiate all constrained type variables"
instance TypeError NoConstraintsErrMsg => Eq (Opaque val rep) where
== :: Opaque val rep -> Opaque val rep -> Bool
(==) = Opaque val rep -> Opaque val rep -> Bool
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Ord (Opaque val rep) where
compare :: Opaque val rep -> Opaque val rep -> Ordering
compare = Opaque val rep -> Opaque val rep -> Ordering
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Num (Opaque val rep) where
+ :: Opaque val rep -> Opaque val rep -> Opaque val rep
(+) = Opaque val rep -> Opaque val rep -> Opaque val rep
forall void. void
underTypeError
* :: Opaque val rep -> Opaque val rep -> Opaque val rep
(*) = Opaque val rep -> Opaque val rep -> Opaque val rep
forall void. void
underTypeError
abs :: Opaque val rep -> Opaque val rep
abs = Opaque val rep -> Opaque val rep
forall void. void
underTypeError
signum :: Opaque val rep -> Opaque val rep
signum = Opaque val rep -> Opaque val rep
forall void. void
underTypeError
fromInteger :: Integer -> Opaque val rep
fromInteger = Integer -> Opaque val rep
forall void. void
underTypeError
negate :: Opaque val rep -> Opaque val rep
negate = Opaque val rep -> Opaque val rep
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Enum (Opaque val rep) where
toEnum :: Int -> Opaque val rep
toEnum = Int -> Opaque val rep
forall void. void
underTypeError
fromEnum :: Opaque val rep -> Int
fromEnum = Opaque val rep -> Int
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Real (Opaque val rep) where
toRational :: Opaque val rep -> Rational
toRational = Opaque val rep -> Rational
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Integral (Opaque val rep) where
quotRem :: Opaque val rep
-> Opaque val rep -> (Opaque val rep, Opaque val rep)
quotRem = Opaque val rep
-> Opaque val rep -> (Opaque val rep, Opaque val rep)
forall void. void
underTypeError
divMod :: Opaque val rep
-> Opaque val rep -> (Opaque val rep, Opaque val rep)
divMod = Opaque val rep
-> Opaque val rep -> (Opaque val rep, Opaque val rep)
forall void. void
underTypeError
toInteger :: Opaque val rep -> Integer
toInteger = Opaque val rep -> Integer
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Bounded (Opaque val rep) where
minBound :: Opaque val rep
minBound = Opaque val rep
forall void. void
underTypeError
maxBound :: Opaque val rep
maxBound = Opaque val rep
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Ix (Opaque val rep) where
range :: (Opaque val rep, Opaque val rep) -> [Opaque val rep]
range = (Opaque val rep, Opaque val rep) -> [Opaque val rep]
forall void. void
underTypeError
index :: (Opaque val rep, Opaque val rep) -> Opaque val rep -> Int
index = (Opaque val rep, Opaque val rep) -> Opaque val rep -> Int
forall void. void
underTypeError
inRange :: (Opaque val rep, Opaque val rep) -> Opaque val rep -> Bool
inRange = (Opaque val rep, Opaque val rep) -> Opaque val rep -> Bool
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Semigroup (Opaque val rep) where
<> :: Opaque val rep -> Opaque val rep -> Opaque val rep
(<>) = Opaque val rep -> Opaque val rep -> Opaque val rep
forall void. void
underTypeError
instance TypeError NoConstraintsErrMsg => Monoid (Opaque val rep) where
mempty :: Opaque val rep
mempty = Opaque val rep
forall void. void
underTypeError
type NoRegularlyAppliedHkVarsMsg =
'Text "Built-in functions are not allowed to have higher-kinded type variables" ':<>:
'Text " applied via regular type application" ':$$:
'Text "To fix this error instantiate all higher-kinded type variables"
instance TypeError NoRegularlyAppliedHkVarsMsg => Functor (Opaque val) where
fmap :: (a -> b) -> Opaque val a -> Opaque val b
fmap = (a -> b) -> Opaque val a -> Opaque val b
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Foldable (Opaque val) where
foldMap :: (a -> m) -> Opaque val a -> m
foldMap = (a -> m) -> Opaque val a -> m
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Traversable (Opaque val) where
traverse :: (a -> f b) -> Opaque val a -> f (Opaque val b)
traverse = (a -> f b) -> Opaque val a -> f (Opaque val b)
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Applicative (Opaque val) where
pure :: a -> Opaque val a
pure = a -> Opaque val a
forall void. void
underTypeError
<*> :: Opaque val (a -> b) -> Opaque val a -> Opaque val b
(<*>) = Opaque val (a -> b) -> Opaque val a -> Opaque val b
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Alternative (Opaque val) where
empty :: Opaque val a
empty = Opaque val a
forall void. void
underTypeError
<|> :: Opaque val a -> Opaque val a -> Opaque val a
(<|>) = Opaque val a -> Opaque val a -> Opaque val a
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Monad (Opaque val) where
>>= :: Opaque val a -> (a -> Opaque val b) -> Opaque val b
(>>=) = Opaque val a -> (a -> Opaque val b) -> Opaque val b
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => MonadIO (Opaque val) where
liftIO :: IO a -> Opaque val a
liftIO = IO a -> Opaque val a
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => MonadPlus (Opaque val) where
mzero :: Opaque val a
mzero = Opaque val a
forall void. void
underTypeError
mplus :: Opaque val a -> Opaque val a -> Opaque val a
mplus = Opaque val a -> Opaque val a -> Opaque val a
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => MonadFail (Opaque val) where
fail :: [Char] -> Opaque val a
fail = [Char] -> Opaque val a
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Bifunctor Opaque where
bimap :: (a -> b) -> (c -> d) -> Opaque a c -> Opaque b d
bimap = (a -> b) -> (c -> d) -> Opaque a c -> Opaque b d
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Bifoldable Opaque where
bifoldMap :: (a -> m) -> (b -> m) -> Opaque a b -> m
bifoldMap = (a -> m) -> (b -> m) -> Opaque a b -> m
forall void. void
underTypeError
instance TypeError NoRegularlyAppliedHkVarsMsg => Bitraversable Opaque where
bitraverse :: (a -> f c) -> (b -> f d) -> Opaque a b -> f (Opaque c d)
bitraverse = (a -> f c) -> (b -> f d) -> Opaque a b -> f (Opaque c d)
forall void. void
underTypeError