module Plutarch.Pretty.Internal.BuiltinConstant (prettyConstant) where
import Data.ByteString (ByteString)
import Data.ByteString.Builder qualified as BSB
import Data.ByteString.Lazy qualified as LBS
import Data.Text (Text)
import Data.Text.Encoding qualified as TxtEnc
import Prettyprinter ((<+>))
import Prettyprinter qualified as PP
import PlutusCore qualified as PLC
import PlutusLedgerApi.V1 qualified as Plutus
import UntypedPlutusCore (DefaultUni)
import Plutarch.Pretty.Internal.Config (indentWidth)
prettyConstant :: PLC.Some (PLC.ValueOf DefaultUni) -> PP.Doc ()
prettyConstant :: Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniInteger a
n)) = forall a ann. Pretty a => a -> Doc ann
PP.pretty a
n
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniByteString a
b)) = forall a ann. Pretty a => a -> Doc ann
PP.pretty forall a b. (a -> b) -> a -> b
$ ByteString -> Text
encodeHex a
b
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniString a
s)) =
forall a ann. Pretty a => a -> Doc ann
PP.pretty forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show a
s
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniUnit a
_)) = Doc ()
"()"
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniBool a
b)) = forall a ann. Pretty a => a -> Doc ann
PP.pretty a
b
prettyConstant (PLC.Some (PLC.ValueOf (PLC.DefaultUniList DefaultUni (Esc @k1 a1)
a) a
l)) =
forall ann. [Doc ann] -> Doc ann
PP.list forall a b. (a -> b) -> a -> b
$
forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap (Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @k1 a1)
a) a
l
prettyConstant (PLC.Some (PLC.ValueOf (PLC.DefaultUniPair DefaultUni (Esc @k3 a2)
a DefaultUni (Esc @k1 a1)
b) ~(a2
x, a1
y))) =
forall ann. [Doc ann] -> Doc ann
PP.tupled
[Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall a b. (a -> b) -> a -> b
$ forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @k3 a2)
a a2
x, Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall a b. (a -> b) -> a -> b
$ forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @k1 a1)
b a1
y]
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniData (Plutus.Constr Integer
ix [Data]
dl))) =
Doc ()
"Σ"
forall a. Semigroup a => a -> a -> a
<> forall a ann. Pretty a => a -> Doc ann
PP.pretty Integer
ix
forall a. Semigroup a => a -> a -> a
<> Doc ()
"."
forall a. Semigroup a => a -> a -> a
<> forall ann. [Doc ann] -> Doc ann
PP.list (Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type Data)
PLC.DefaultUniData forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [Data]
dl)
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniData (Plutus.Map [(Data, Data)]
ascList))) =
forall ann. Doc ann -> Doc ann
PP.group
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
PP.encloseSep (forall ann. Doc ann -> Doc ann -> Doc ann
PP.flatAlt Doc ()
"{ " Doc ()
"{") (forall ann. Doc ann -> Doc ann -> Doc ann
PP.flatAlt Doc ()
" }" Doc ()
"}") Doc ()
", "
forall a b. (a -> b) -> a -> b
$ forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
fmap
( \(Data
a, Data
b) ->
forall ann. Int -> Doc ann -> Doc ann
PP.hang Int
indentWidth forall a b. (a -> b) -> a -> b
$
forall ann. [Doc ann] -> Doc ann
PP.sep
[ Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant (forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some (forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type Data)
PLC.DefaultUniData Data
a)) forall ann. Doc ann -> Doc ann -> Doc ann
<+> Doc ()
"="
, Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall a b. (a -> b) -> a -> b
$ forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall a b. (a -> b) -> a -> b
$ forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type Data)
PLC.DefaultUniData Data
b
]
)
[(Data, Data)]
ascList
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniData (Plutus.List [Data]
l))) =
Doc ()
"#" forall a. Semigroup a => a -> a -> a
<> forall ann. [Doc ann] -> Doc ann
PP.list (Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type Data)
PLC.DefaultUniData forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> [Data]
l)
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniData (Plutus.B ByteString
b))) =
Doc ()
"#" forall a. Semigroup a => a -> a -> a
<> Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant (forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall a b. (a -> b) -> a -> b
$ forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type ByteString)
PLC.DefaultUniByteString ByteString
b)
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
PLC.DefaultUniData (Plutus.I Integer
i))) =
Doc ()
"#" forall a. Semigroup a => a -> a -> a
<> Some @Type (ValueOf DefaultUni) -> Doc ()
prettyConstant (forall {k} (tag :: k -> Type) (a :: k). tag a -> Some @k tag
PLC.Some forall a b. (a -> b) -> a -> b
$ forall (uni :: Type -> Type) a.
uni (Esc @Type a) -> a -> ValueOf uni a
PLC.ValueOf DefaultUni (Esc @Type Integer)
PLC.DefaultUniInteger Integer
i)
prettyConstant (PLC.Some (PLC.ValueOf DefaultUni (Esc @Type a)
uni a
_)) =
forall a. HasCallStack => String -> a
error forall a b. (a -> b) -> a -> b
$ String
"prettyConstant(impossible): " forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> String
show DefaultUni (Esc @Type a)
uni
encodeHex :: ByteString -> Text
encodeHex :: ByteString -> Text
encodeHex = (Text
"0x" <>) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
TxtEnc.decodeUtf8 forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
LBS.toStrict forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
BSB.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Builder
BSB.byteStringHex