-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ

-- | Michelson contract in untyped model.

module Michelson.Untyped.Contract
  ( Storage
  , Contract' (..)
  ) where

import Data.Aeson.TH (deriveJSON)
import Data.Data (Data(..))
import Formatting.Buildable (Buildable(build))
import Text.PrettyPrint.Leijen.Text (nest, semi, (<$$>), (<+>))

import Michelson.Printer.Util
  (Prettier(..), RenderDoc(..), assertParensNotNeeded, buildRenderDoc, needsParens, renderOpsList)
import Michelson.Untyped.Type (ParameterType(..), Type(..))
import Util.Aeson

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

instance NFData op => NFData (Contract' op)

instance (RenderDoc op) => RenderDoc (Contract' op) where
  renderDoc :: RenderContext -> Contract' op -> Doc
renderDoc pn :: RenderContext
pn (Contract parameter :: ParameterType
parameter storage :: Storage
storage code :: [op]
code) = RenderContext -> Doc -> Doc
forall a. RenderContext -> a -> a
assertParensNotNeeded RenderContext
pn (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$
    "parameter" Doc -> Doc -> Doc
<+> RenderContext -> Prettier ParameterType -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
needsParens (ParameterType -> Prettier ParameterType
forall a. a -> Prettier a
Prettier ParameterType
parameter)  Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
semi Doc -> Doc -> Doc
<$$>
    "storage"   Doc -> Doc -> Doc
<+> RenderContext -> Prettier Storage -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
needsParens (Storage -> Prettier Storage
forall a. a -> Prettier a
Prettier Storage
storage)    Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
semi Doc -> Doc -> Doc
<$$>
    "code"      Doc -> Doc -> Doc
<+> Int -> Doc -> Doc
nest (Text -> Int
forall t. Container t => t -> Int
length ("code {" :: Text)) (Bool -> [op] -> Doc
forall op. RenderDoc op => Bool -> [op] -> Doc
renderOpsList Bool
False [op]
code Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
semi)

instance RenderDoc op => Buildable (Contract' op) where
  build :: Contract' op -> Builder
build = Contract' op -> Builder
forall a. RenderDoc a => a -> Builder
buildRenderDoc

deriveJSON morleyAesonOptions ''Contract'