-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Untyped Michelson values (i. e. type of a value is not statically known).

module Morley.Michelson.Untyped.Value
  ( Value' (..)
  , Elt (..)
  -- Internal types to avoid orphan instances
  , InternalByteString(..)
  , unInternalByteString
  , renderValuesList
  , renderSome
  , renderNone
  , renderLeft
  , renderRight
  , renderPair
  , renderElt'
  ) where

import Prelude hiding (group)

import Data.Aeson (FromJSON(..), ToJSON(..), withText)
import Data.Aeson.Types (genericParseJSON, genericToEncoding, genericToJSON)
import Data.Data (Data(..))
import Fmt (Doc, build, hexF, (<+>))
import Fmt qualified as PP ((<$>))
import Prettyprinter
  (align, dquotes, encloseSep, group, hang, lbrace, rbrace, semi, sep, softline, space)
import Text.Hex (decodeHex, encodeHex)

import Morley.Michelson.Printer.Util
import Morley.Michelson.Text
import Morley.Michelson.Untyped.HoistInstr
import Morley.Util.Aeson

data Value' f op =
    ValueInt     Integer
  | ValueString  MText
  | ValueBytes   InternalByteString
  | ValueUnit
  | ValueTrue
  | ValueFalse
  | ValuePair    (Value' f op) (Value' f op)
  | ValueLeft    (Value' f op)
  | ValueRight   (Value' f op)
  | ValueSome    (Value' f op)
  | ValueNone
  | ValueNil
  | ValueSeq     (NonEmpty $ Value' f op)
  -- ^ A sequence of elements: can be a list, a set or a pair.
  -- We can't distinguish lists and sets during parsing.
  | ValueMap     (NonEmpty $ Elt f op)
  | ValueLambda  (f op)
  -- ^ Invariant: contains non-empty list of instructions. For an empty list,
  -- use 'ValueNil'.
  | ValueLamRec  (f op)
  deriving stock (Value' f op -> Value' f op -> Bool
(Value' f op -> Value' f op -> Bool)
-> (Value' f op -> Value' f op -> Bool) -> Eq (Value' f op)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Value' f op -> Value' f op -> Bool
$c== :: forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Value' f op -> Value' f op -> Bool
== :: Value' f op -> Value' f op -> Bool
$c/= :: forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Value' f op -> Value' f op -> Bool
/= :: Value' f op -> Value' f op -> Bool
Eq, Int -> Value' f op -> ShowS
[Value' f op] -> ShowS
Value' f op -> String
(Int -> Value' f op -> ShowS)
-> (Value' f op -> String)
-> ([Value' f op] -> ShowS)
-> Show (Value' f op)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (f :: k -> *) (op :: k).
Show (f op) =>
Int -> Value' f op -> ShowS
forall k (f :: k -> *) (op :: k).
Show (f op) =>
[Value' f op] -> ShowS
forall k (f :: k -> *) (op :: k).
Show (f op) =>
Value' f op -> String
$cshowsPrec :: forall k (f :: k -> *) (op :: k).
Show (f op) =>
Int -> Value' f op -> ShowS
showsPrec :: Int -> Value' f op -> ShowS
$cshow :: forall k (f :: k -> *) (op :: k).
Show (f op) =>
Value' f op -> String
show :: Value' f op -> String
$cshowList :: forall k (f :: k -> *) (op :: k).
Show (f op) =>
[Value' f op] -> ShowS
showList :: [Value' f op] -> ShowS
Show, (forall a b. (a -> b) -> Value' f a -> Value' f b)
-> (forall a b. a -> Value' f b -> Value' f a)
-> Functor (Value' f)
forall a b. a -> Value' f b -> Value' f a
forall a b. (a -> b) -> Value' f a -> Value' f b
forall (f :: * -> *) a b.
Functor f =>
a -> Value' f b -> Value' f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Value' f a -> Value' f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Value' f a -> Value' f b
fmap :: forall a b. (a -> b) -> Value' f a -> Value' f b
$c<$ :: forall (f :: * -> *) a b.
Functor f =>
a -> Value' f b -> Value' f a
<$ :: forall a b. a -> Value' f b -> Value' f a
Functor, Typeable (Value' f op)
Typeable (Value' f op)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Value' f op -> c (Value' f op))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Value' f op))
-> (Value' f op -> Constr)
-> (Value' f op -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Value' f op)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Value' f op)))
-> ((forall b. Data b => b -> b) -> Value' f op -> Value' f op)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Value' f op -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Value' f op -> r)
-> (forall u. (forall d. Data d => d -> u) -> Value' f op -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> Value' f op -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op))
-> Data (Value' f op)
Value' f op -> Constr
Value' f op -> DataType
(forall b. Data b => b -> b) -> Value' f op -> Value' f op
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Value' f op -> u
forall u. (forall d. Data d => d -> u) -> Value' f op -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
forall {k} {f :: k -> *} {op :: k}.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Typeable (Value' f op)
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Value' f op -> Constr
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Value' f op -> DataType
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b. Data b => b -> b) -> Value' f op -> Value' f op
forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Int -> (forall d. Data d => d -> u) -> Value' f op -> u
forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d. Data d => d -> u) -> Value' f op -> [u]
forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Monad m) =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Value' f op)
forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value' f op -> c (Value' f op)
forall k (f :: k -> *) (op :: k) (t :: * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Value' f op))
forall k (f :: k -> *) (op :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Value' f op))
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Value' f op)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value' f op -> c (Value' f op)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Value' f op))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Value' f op))
$cgfoldl :: forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value' f op -> c (Value' f op)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Value' f op -> c (Value' f op)
$cgunfold :: forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Value' f op)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Value' f op)
$ctoConstr :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Value' f op -> Constr
toConstr :: Value' f op -> Constr
$cdataTypeOf :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Value' f op -> DataType
dataTypeOf :: Value' f op -> DataType
$cdataCast1 :: forall k (f :: k -> *) (op :: k) (t :: * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Value' f op))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Value' f op))
$cdataCast2 :: forall k (f :: k -> *) (op :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Value' f op))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (Value' f op))
$cgmapT :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b. Data b => b -> b) -> Value' f op -> Value' f op
gmapT :: (forall b. Data b => b -> b) -> Value' f op -> Value' f op
$cgmapQl :: forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
$cgmapQr :: forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Value' f op -> r
$cgmapQ :: forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d. Data d => d -> u) -> Value' f op -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Value' f op -> [u]
$cgmapQi :: forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Int -> (forall d. Data d => d -> u) -> Value' f op -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Value' f op -> u
$cgmapM :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Monad m) =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
$cgmapMp :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
$cgmapMo :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Value' f op -> m (Value' f op)
Data, (forall x. Value' f op -> Rep (Value' f op) x)
-> (forall x. Rep (Value' f op) x -> Value' f op)
-> Generic (Value' f op)
forall x. Rep (Value' f op) x -> Value' f op
forall x. Value' f op -> Rep (Value' f op) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (f :: k -> *) (op :: k) x.
Rep (Value' f op) x -> Value' f op
forall k (f :: k -> *) (op :: k) x.
Value' f op -> Rep (Value' f op) x
$cfrom :: forall k (f :: k -> *) (op :: k) x.
Value' f op -> Rep (Value' f op) x
from :: forall x. Value' f op -> Rep (Value' f op) x
$cto :: forall k (f :: k -> *) (op :: k) x.
Rep (Value' f op) x -> Value' f op
to :: forall x. Rep (Value' f op) x -> Value' f op
Generic)

instance HoistInstr Value' where
  hoistInstr :: forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f = \case
    ValueInt Integer
n -> Integer -> Value' g a
forall {k} (f :: k -> *) (op :: k). Integer -> Value' f op
ValueInt Integer
n
    ValueString MText
t -> MText -> Value' g a
forall {k} (f :: k -> *) (op :: k). MText -> Value' f op
ValueString MText
t
    ValueBytes InternalByteString
bs -> InternalByteString -> Value' g a
forall {k} (f :: k -> *) (op :: k).
InternalByteString -> Value' f op
ValueBytes InternalByteString
bs
    Value' f a
ValueUnit -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op
ValueUnit
    Value' f a
ValueTrue -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op
ValueTrue
    Value' f a
ValueFalse -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op
ValueFalse
    Value' f a
ValueNone -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op
ValueNone
    Value' f a
ValueNil -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op
ValueNil
    ValuePair Value' f a
l Value' f a
r -> Value' g a -> Value' g a -> Value' g a
forall {k} (f :: k -> *) (op :: k).
Value' f op -> Value' f op -> Value' f op
ValuePair ((f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
l) ((f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
r)
    ValueLeft Value' f a
l -> Value' g a -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op -> Value' f op
ValueLeft (Value' g a -> Value' g a) -> Value' g a -> Value' g a
forall a b. (a -> b) -> a -> b
$ (f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
l
    ValueRight Value' f a
l -> Value' g a -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op -> Value' f op
ValueRight (Value' g a -> Value' g a) -> Value' g a -> Value' g a
forall a b. (a -> b) -> a -> b
$ (f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
l
    ValueSome Value' f a
l -> Value' g a -> Value' g a
forall {k} (f :: k -> *) (op :: k). Value' f op -> Value' f op
ValueSome (Value' g a -> Value' g a) -> Value' g a -> Value' g a
forall a b. (a -> b) -> a -> b
$ (f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
l
    ValueSeq NonEmpty $ Value' f a
vs -> (NonEmpty $ Value' g a) -> Value' g a
forall {k} (f :: k -> *) (op :: k).
(NonEmpty $ Value' f op) -> Value' f op
ValueSeq ((NonEmpty $ Value' g a) -> Value' g a)
-> (NonEmpty $ Value' g a) -> Value' g a
forall a b. (a -> b) -> a -> b
$ (f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f (Value' f a -> Value' g a)
-> (NonEmpty $ Value' f a) -> NonEmpty $ Value' g a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty $ Value' f a
vs
    ValueMap NonEmpty $ Elt f a
els -> (NonEmpty $ Elt g a) -> Value' g a
forall {k} (f :: k -> *) (op :: k).
(NonEmpty $ Elt f op) -> Value' f op
ValueMap ((NonEmpty $ Elt g a) -> Value' g a)
-> (NonEmpty $ Elt g a) -> Value' g a
forall a b. (a -> b) -> a -> b
$ (f a -> g a) -> Elt f a -> Elt g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Elt f a -> Elt g a
hoistInstr f a -> g a
f (Elt f a -> Elt g a) -> (NonEmpty $ Elt f a) -> NonEmpty $ Elt g a
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> NonEmpty $ Elt f a
els
    ValueLambda f a
ops -> g a -> Value' g a
forall {k} (f :: k -> *) (op :: k). f op -> Value' f op
ValueLambda (g a -> Value' g a) -> g a -> Value' g a
forall a b. (a -> b) -> a -> b
$ f a -> g a
f f a
ops
    ValueLamRec f a
ops -> g a -> Value' g a
forall {k} (f :: k -> *) (op :: k). f op -> Value' f op
ValueLamRec (g a -> Value' g a) -> g a -> Value' g a
forall a b. (a -> b) -> a -> b
$ f a -> g a
f f a
ops

instance NFData (f op) => NFData (Value' f op)

data Elt f op = Elt (Value' f op) (Value' f op)
  deriving stock (Elt f op -> Elt f op -> Bool
(Elt f op -> Elt f op -> Bool)
-> (Elt f op -> Elt f op -> Bool) -> Eq (Elt f op)
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Elt f op -> Elt f op -> Bool
$c== :: forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Elt f op -> Elt f op -> Bool
== :: Elt f op -> Elt f op -> Bool
$c/= :: forall k (f :: k -> *) (op :: k).
Eq (f op) =>
Elt f op -> Elt f op -> Bool
/= :: Elt f op -> Elt f op -> Bool
Eq, Int -> Elt f op -> ShowS
[Elt f op] -> ShowS
Elt f op -> String
(Int -> Elt f op -> ShowS)
-> (Elt f op -> String) -> ([Elt f op] -> ShowS) -> Show (Elt f op)
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall k (f :: k -> *) (op :: k).
Show (f op) =>
Int -> Elt f op -> ShowS
forall k (f :: k -> *) (op :: k).
Show (f op) =>
[Elt f op] -> ShowS
forall k (f :: k -> *) (op :: k). Show (f op) => Elt f op -> String
$cshowsPrec :: forall k (f :: k -> *) (op :: k).
Show (f op) =>
Int -> Elt f op -> ShowS
showsPrec :: Int -> Elt f op -> ShowS
$cshow :: forall k (f :: k -> *) (op :: k). Show (f op) => Elt f op -> String
show :: Elt f op -> String
$cshowList :: forall k (f :: k -> *) (op :: k).
Show (f op) =>
[Elt f op] -> ShowS
showList :: [Elt f op] -> ShowS
Show, (forall a b. (a -> b) -> Elt f a -> Elt f b)
-> (forall a b. a -> Elt f b -> Elt f a) -> Functor (Elt f)
forall a b. a -> Elt f b -> Elt f a
forall a b. (a -> b) -> Elt f a -> Elt f b
forall (f :: * -> *) a b. Functor f => a -> Elt f b -> Elt f a
forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Elt f a -> Elt f b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall (f :: * -> *) a b.
Functor f =>
(a -> b) -> Elt f a -> Elt f b
fmap :: forall a b. (a -> b) -> Elt f a -> Elt f b
$c<$ :: forall (f :: * -> *) a b. Functor f => a -> Elt f b -> Elt f a
<$ :: forall a b. a -> Elt f b -> Elt f a
Functor, Typeable (Elt f op)
Typeable (Elt f op)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> Elt f op -> c (Elt f op))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (Elt f op))
-> (Elt f op -> Constr)
-> (Elt f op -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (Elt f op)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (Elt f op)))
-> ((forall b. Data b => b -> b) -> Elt f op -> Elt f op)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> Elt f op -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> Elt f op -> r)
-> (forall u. (forall d. Data d => d -> u) -> Elt f op -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> Elt f op -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op))
-> Data (Elt f op)
Elt f op -> Constr
Elt f op -> DataType
(forall b. Data b => b -> b) -> Elt f op -> Elt f op
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u. Int -> (forall d. Data d => d -> u) -> Elt f op -> u
forall u. (forall d. Data d => d -> u) -> Elt f op -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
forall {k} {f :: k -> *} {op :: k}.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Typeable (Elt f op)
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Elt f op -> Constr
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Elt f op -> DataType
forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b. Data b => b -> b) -> Elt f op -> Elt f op
forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Int -> (forall d. Data d => d -> u) -> Elt f op -> u
forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d. Data d => d -> u) -> Elt f op -> [u]
forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Monad m) =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Elt f op)
forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Elt f op -> c (Elt f op)
forall k (f :: k -> *) (op :: k) (t :: * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Elt f op))
forall k (f :: k -> *) (op :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Elt f op))
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Elt f op)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Elt f op -> c (Elt f op)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Elt f op))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Elt f op))
$cgfoldl :: forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Elt f op -> c (Elt f op)
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> Elt f op -> c (Elt f op)
$cgunfold :: forall k (f :: k -> *) (op :: k) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Elt f op)
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (Elt f op)
$ctoConstr :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Elt f op -> Constr
toConstr :: Elt f op -> Constr
$cdataTypeOf :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Elt f op -> DataType
dataTypeOf :: Elt f op -> DataType
$cdataCast1 :: forall k (f :: k -> *) (op :: k) (t :: * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (Elt f op))
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (Elt f op))
$cdataCast2 :: forall k (f :: k -> *) (op :: k) (t :: * -> * -> *) (c :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Elt f op))
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Elt f op))
$cgmapT :: forall k (f :: k -> *) (op :: k).
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall b. Data b => b -> b) -> Elt f op -> Elt f op
gmapT :: (forall b. Data b => b -> b) -> Elt f op -> Elt f op
$cgmapQl :: forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
$cgmapQr :: forall k (f :: k -> *) (op :: k) r r'.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> Elt f op -> r
$cgmapQ :: forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
(forall d. Data d => d -> u) -> Elt f op -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> Elt f op -> [u]
$cgmapQi :: forall k (f :: k -> *) (op :: k) u.
(Typeable op, Typeable f, Typeable k, Data (f op)) =>
Int -> (forall d. Data d => d -> u) -> Elt f op -> u
gmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> Elt f op -> u
$cgmapM :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), Monad m) =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
$cgmapMp :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
$cgmapMo :: forall k (f :: k -> *) (op :: k) (m :: * -> *).
(Typeable op, Typeable f, Typeable k, Data (f op), MonadPlus m) =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> Elt f op -> m (Elt f op)
Data, (forall x. Elt f op -> Rep (Elt f op) x)
-> (forall x. Rep (Elt f op) x -> Elt f op) -> Generic (Elt f op)
forall x. Rep (Elt f op) x -> Elt f op
forall x. Elt f op -> Rep (Elt f op) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall k (f :: k -> *) (op :: k) x. Rep (Elt f op) x -> Elt f op
forall k (f :: k -> *) (op :: k) x. Elt f op -> Rep (Elt f op) x
$cfrom :: forall k (f :: k -> *) (op :: k) x. Elt f op -> Rep (Elt f op) x
from :: forall x. Elt f op -> Rep (Elt f op) x
$cto :: forall k (f :: k -> *) (op :: k) x. Rep (Elt f op) x -> Elt f op
to :: forall x. Rep (Elt f op) x -> Elt f op
Generic)

instance HoistInstr Elt where
  hoistInstr :: forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Elt f a -> Elt g a
hoistInstr f a -> g a
f (Elt Value' f a
l Value' f a
r) = Value' g a -> Value' g a -> Elt g a
forall {k} (f :: k -> *) (op :: k).
Value' f op -> Value' f op -> Elt f op
Elt ((f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
l) ((f a -> g a) -> Value' f a -> Value' g a
forall {k} (n :: (k -> *) -> k -> *) (f :: k -> *) (a :: k)
       (g :: k -> *).
HoistInstr n =>
(f a -> g a) -> n f a -> n g a
forall (f :: k -> *) (a :: k) (g :: k -> *).
(f a -> g a) -> Value' f a -> Value' g a
hoistInstr f a -> g a
f Value' f a
r)

instance NFData (f op) => NFData (Elt f op)

-- | ByteString does not have an instance for ToJSON and FromJSON, to
-- avoid orphan type class instances, make a new type wrapper around it.
newtype InternalByteString = InternalByteString ByteString
  deriving stock (Typeable InternalByteString
Typeable InternalByteString
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> InternalByteString
    -> c InternalByteString)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c InternalByteString)
-> (InternalByteString -> Constr)
-> (InternalByteString -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c InternalByteString))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c InternalByteString))
-> ((forall b. Data b => b -> b)
    -> InternalByteString -> InternalByteString)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> InternalByteString -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> InternalByteString -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> InternalByteString -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> InternalByteString -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> InternalByteString -> m InternalByteString)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InternalByteString -> m InternalByteString)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> InternalByteString -> m InternalByteString)
-> Data InternalByteString
InternalByteString -> Constr
InternalByteString -> DataType
(forall b. Data b => b -> b)
-> InternalByteString -> InternalByteString
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> InternalByteString -> u
forall u. (forall d. Data d => d -> u) -> InternalByteString -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InternalByteString
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InternalByteString
-> c InternalByteString
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c InternalByteString)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InternalByteString)
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InternalByteString
-> c InternalByteString
gfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> InternalByteString
-> c InternalByteString
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InternalByteString
gunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c InternalByteString
$ctoConstr :: InternalByteString -> Constr
toConstr :: InternalByteString -> Constr
$cdataTypeOf :: InternalByteString -> DataType
dataTypeOf :: InternalByteString -> DataType
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c InternalByteString)
dataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c InternalByteString)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InternalByteString)
dataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c InternalByteString)
$cgmapT :: (forall b. Data b => b -> b)
-> InternalByteString -> InternalByteString
gmapT :: (forall b. Data b => b -> b)
-> InternalByteString -> InternalByteString
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
gmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
gmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> InternalByteString -> r
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> InternalByteString -> [u]
gmapQ :: forall u. (forall d. Data d => d -> u) -> InternalByteString -> [u]
$cgmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> InternalByteString -> u
gmapQi :: forall u.
Int -> (forall d. Data d => d -> u) -> InternalByteString -> u
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
gmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
gmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
gmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> InternalByteString -> m InternalByteString
Data, InternalByteString -> InternalByteString -> Bool
(InternalByteString -> InternalByteString -> Bool)
-> (InternalByteString -> InternalByteString -> Bool)
-> Eq InternalByteString
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: InternalByteString -> InternalByteString -> Bool
== :: InternalByteString -> InternalByteString -> Bool
$c/= :: InternalByteString -> InternalByteString -> Bool
/= :: InternalByteString -> InternalByteString -> Bool
Eq, Int -> InternalByteString -> ShowS
[InternalByteString] -> ShowS
InternalByteString -> String
(Int -> InternalByteString -> ShowS)
-> (InternalByteString -> String)
-> ([InternalByteString] -> ShowS)
-> Show InternalByteString
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> InternalByteString -> ShowS
showsPrec :: Int -> InternalByteString -> ShowS
$cshow :: InternalByteString -> String
show :: InternalByteString -> String
$cshowList :: [InternalByteString] -> ShowS
showList :: [InternalByteString] -> ShowS
Show, (forall x. InternalByteString -> Rep InternalByteString x)
-> (forall x. Rep InternalByteString x -> InternalByteString)
-> Generic InternalByteString
forall x. Rep InternalByteString x -> InternalByteString
forall x. InternalByteString -> Rep InternalByteString x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. InternalByteString -> Rep InternalByteString x
from :: forall x. InternalByteString -> Rep InternalByteString x
$cto :: forall x. Rep InternalByteString x -> InternalByteString
to :: forall x. Rep InternalByteString x -> InternalByteString
Generic)

instance NFData InternalByteString

unInternalByteString :: InternalByteString -> ByteString
unInternalByteString :: InternalByteString -> ByteString
unInternalByteString (InternalByteString ByteString
bs) = ByteString
bs

instance (Foldable f, RenderDoc op) => RenderDoc (Value' f op) where
  renderDoc :: RenderContext -> Value' f op -> Doc
renderDoc RenderContext
pn =
    \case
      Value' f op
ValueNil       -> Doc
"{ }"
      ValueInt Integer
x     -> Integer -> Doc
forall a. Buildable a => a -> Doc
build Integer
x
      ValueString MText
x  -> Doc -> Doc
forall ann. Doc ann -> Doc ann
dquotes (Text -> Doc
forall a. Buildable a => a -> Doc
build (Text -> Doc) -> Text -> Doc
forall a b. (a -> b) -> a -> b
$ MText -> Text
writeMText MText
x)
      ValueBytes InternalByteString
xs  -> Doc
"0x" Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Doc
forall a. FormatAsHex a => a -> Doc
hexF (ByteString -> Doc)
-> (InternalByteString -> ByteString) -> InternalByteString -> Doc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InternalByteString -> ByteString
unInternalByteString (InternalByteString -> Doc) -> InternalByteString -> Doc
forall a b. (a -> b) -> a -> b
$ InternalByteString
xs)
      Value' f op
ValueUnit      -> Doc
"Unit"
      Value' f op
ValueTrue      -> Doc
"True"
      Value' f op
ValueFalse     -> Doc
"False"
      ValuePair Value' f op
l Value' f op
r  -> RenderContext
-> (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderPair  RenderContext
pn ((RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
l) ((RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
r)
      ValueLeft Value' f op
l    -> RenderContext -> (RenderContext -> Doc) -> Doc
renderLeft  RenderContext
pn ((RenderContext -> Doc) -> Doc) -> (RenderContext -> Doc) -> Doc
forall a b. (a -> b) -> a -> b
$ (RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
l
      ValueRight Value' f op
r   -> RenderContext -> (RenderContext -> Doc) -> Doc
renderRight RenderContext
pn ((RenderContext -> Doc) -> Doc) -> (RenderContext -> Doc) -> Doc
forall a b. (a -> b) -> a -> b
$ (RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
r
      ValueSome Value' f op
x    -> RenderContext -> (RenderContext -> Doc) -> Doc
renderSome  RenderContext
pn ((RenderContext -> Doc) -> Doc) -> (RenderContext -> Doc) -> Doc
forall a b. (a -> b) -> a -> b
$ (RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
x
      Value' f op
ValueNone      -> Doc
renderNone
      ValueSeq NonEmpty $ Value' f op
xs    -> (Value' f op -> Doc) -> (NonEmpty $ Value' f op) -> Doc
forall e. (e -> Doc) -> NonEmpty e -> Doc
renderValuesList (RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
doesntNeedParens) NonEmpty $ Value' f op
xs
      ValueMap NonEmpty $ Elt f op
xs    -> (Elt f op -> Doc) -> (NonEmpty $ Elt f op) -> Doc
forall e. (e -> Doc) -> NonEmpty e -> Doc
renderValuesList Elt f op -> Doc
forall (f :: * -> *) op.
(Foldable f, RenderDoc op) =>
Elt f op -> Doc
renderElt NonEmpty $ Elt f op
xs
      ValueLambda f op
xs -> Bool -> f op -> Doc
forall op (f :: * -> *).
(RenderDoc op, Foldable f) =>
Bool -> f op -> Doc
renderOpsList Bool
False f op
xs
      ValueLamRec f op
xs -> Doc
"Lambda_rec" Doc -> Doc -> Doc
<+> Doc -> Doc
forall ann. Doc ann -> Doc ann
align (Bool -> f op -> Doc
forall op (f :: * -> *).
(RenderDoc op, Foldable f) =>
Bool -> f op -> Doc
renderOpsList Bool
False f op
xs)

-- | Helper function to render @None@ @Value@
renderNone :: Doc
renderNone :: Doc
renderNone = Doc
"None"

-- | Helper functions to render @Value@s
renderSome, renderLeft, renderRight :: RenderContext -> (RenderContext -> Doc) -> Doc
renderSome :: RenderContext -> (RenderContext -> Doc) -> Doc
renderSome  RenderContext
pn RenderContext -> Doc
render = RenderContext -> Doc -> Doc
renderContainer RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
"Some" Doc -> Doc -> Doc
PP.<$> RenderContext -> Doc
render RenderContext
needsParens
renderLeft :: RenderContext -> (RenderContext -> Doc) -> Doc
renderLeft  RenderContext
pn RenderContext -> Doc
render = RenderContext -> Doc -> Doc
renderContainer RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
"Left" Doc -> Doc -> Doc
PP.<$> RenderContext -> Doc
render RenderContext
needsParens
renderRight :: RenderContext -> (RenderContext -> Doc) -> Doc
renderRight RenderContext
pn RenderContext -> Doc
render = RenderContext -> Doc -> Doc
renderContainer RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
"Right" Doc -> Doc -> Doc
PP.<$> RenderContext -> Doc
render RenderContext
needsParens

-- | Helper function to format container values such as @Some@ and @Right@.
renderContainer :: RenderContext -> Doc -> Doc
renderContainer :: RenderContext -> Doc -> Doc
renderContainer RenderContext
pn Doc
doc = RenderContext -> Doc -> Doc
addParensMultiline RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc
forall ann. Doc ann -> Doc ann
group (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Int -> Doc -> Doc
forall ann. Int -> Doc ann -> Doc ann
hang Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc
doc

-- | Helper function to render @Pair@ @Value@
renderPair :: RenderContext -> (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderPair :: RenderContext
-> (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderPair RenderContext
pn RenderContext -> Doc
l RenderContext -> Doc
r = RenderContext -> Doc -> Doc
addParensMultiline RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Int -> Doc -> Doc
forall ann. Int -> Doc ann -> Doc ann
hang Int
2 (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
  [Doc] -> Doc
forall ann. [Doc ann] -> Doc ann
sep [Doc
"Pair", RenderContext -> Doc
l RenderContext
needsParens, RenderContext -> Doc
r RenderContext
needsParens]

-- | Helper function to render @Elt@
renderElt' :: (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderElt' :: (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderElt' RenderContext -> Doc
l RenderContext -> Doc
r = Doc
"Elt" Doc -> Doc -> Doc
<+> RenderContext -> Doc
l RenderContext
needsParens Doc -> Doc -> Doc
<+> RenderContext -> Doc
r RenderContext
needsParens

renderElt :: (Foldable f, RenderDoc op) => Elt f op -> Doc
renderElt :: forall (f :: * -> *) op.
(Foldable f, RenderDoc op) =>
Elt f op -> Doc
renderElt (Elt Value' f op
k Value' f op
v) = (RenderContext -> Doc) -> (RenderContext -> Doc) -> Doc
renderElt' ((RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
k) ((RenderContext -> Value' f op -> Doc)
-> Value' f op -> RenderContext -> Doc
forall a b c. (a -> b -> c) -> b -> a -> c
flip RenderContext -> Value' f op -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc Value' f op
v)

instance (Foldable f, RenderDoc op) => RenderDoc (Elt f op) where
  renderDoc :: RenderContext -> Elt f op -> Doc
renderDoc RenderContext
_ = Elt f op -> Doc
forall (f :: * -> *) op.
(Foldable f, RenderDoc op) =>
Elt f op -> Doc
renderElt

-- | A helper function that renders a 'NonEmpty' list of items in Michelson-readable format,
-- given a rendering function for a single item.
renderValuesList :: (e -> Doc) -> NonEmpty e -> Doc
renderValuesList :: forall e. (e -> Doc) -> NonEmpty e -> Doc
renderValuesList e -> Doc
renderElem (NonEmpty e -> [Element (NonEmpty e)]
forall t. Container t => t -> [Element t]
toList -> [Element (NonEmpty e)]
es) =
  Doc -> Doc
forall ann. Doc ann -> Doc ann
align (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> Doc -> Doc -> [Doc] -> Doc
forall ann. Doc ann -> Doc ann -> Doc ann -> [Doc ann] -> Doc ann
encloseSep (Doc
forall ann. Doc ann
lbrace Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
forall ann. Doc ann
space) (Doc
forall ann. Doc ann
softline Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
forall ann. Doc ann
rbrace) (Doc
forall ann. Doc ann
semi Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
forall ann. Doc ann
space) ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$
    e -> Doc
renderElem (e -> Doc) -> [e] -> [Doc]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [e]
[Element (NonEmpty e)]
es

----------------------------------------------------------------------------
-- JSON serialization
----------------------------------------------------------------------------

-- it is not possible to derives these automatically because
-- ByteString does not have a ToJSON or FromJSON instance

instance ToJSON InternalByteString where
  toJSON :: InternalByteString -> Value
toJSON = Text -> Value
forall a. ToJSON a => a -> Value
toJSON (Text -> Value)
-> (InternalByteString -> Text) -> InternalByteString -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Text
encodeHex (ByteString -> Text)
-> (InternalByteString -> ByteString) -> InternalByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. InternalByteString -> ByteString
unInternalByteString

instance FromJSON InternalByteString where
  parseJSON :: Value -> Parser InternalByteString
parseJSON =
    String
-> (Text -> Parser InternalByteString)
-> Value
-> Parser InternalByteString
forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"Hex-encoded bytestring" ((Text -> Parser InternalByteString)
 -> Value -> Parser InternalByteString)
-> (Text -> Parser InternalByteString)
-> Value
-> Parser InternalByteString
forall a b. (a -> b) -> a -> b
$ \Text
t ->
      case Text -> Maybe ByteString
decodeHex Text
t of
        Maybe ByteString
Nothing -> String -> Parser InternalByteString
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid hex encoding"
        Just ByteString
res -> InternalByteString -> Parser InternalByteString
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> InternalByteString
InternalByteString ByteString
res)


instance FromJSON (f op) => FromJSON (Value' f op) where
    parseJSON :: Value -> Parser (Value' f op)
parseJSON = Options -> Value -> Parser (Value' f op)
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
morleyAesonOptions
instance ToJSON (f op) => ToJSON (Value' f op) where
    toJSON :: Value' f op -> Value
toJSON     = Options -> Value' f op -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
morleyAesonOptions
    toEncoding :: Value' f op -> Encoding
toEncoding = Options -> Value' f op -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
morleyAesonOptions

instance FromJSON (f op) => FromJSON (Elt f op) where
    parseJSON :: Value -> Parser (Elt f op)
parseJSON = Options -> Value -> Parser (Elt f op)
forall a.
(Generic a, GFromJSON Zero (Rep a)) =>
Options -> Value -> Parser a
genericParseJSON Options
morleyAesonOptions
instance ToJSON (f op) => ToJSON (Elt f op) where
    toJSON :: Elt f op -> Value
toJSON     = Options -> Elt f op -> Value
forall a.
(Generic a, GToJSON' Value Zero (Rep a)) =>
Options -> a -> Value
genericToJSON Options
morleyAesonOptions
    toEncoding :: Elt f op -> Encoding
toEncoding = Options -> Elt f op -> Encoding
forall a.
(Generic a, GToJSON' Encoding Zero (Rep a)) =>
Options -> a -> Encoding
genericToEncoding Options
morleyAesonOptions