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

-- | Michelson contract in untyped model.

module Michelson.Untyped.Contract
  ( EntriesOrder (..)
  , canonicalEntriesOrder
  , entriesOrderToInt
  , mapEntriesOrdered

  , ContractBlock (..)
  , orderContractBlock

  , Contract' (..)
  , Storage
  ) where

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

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

-- | Top-level entries order of the contract.
-- This is preserved due to the fact that it affects
-- the output of pretty-printing and serializing contract.

-- Each constructors is created by the order of the first letter of
-- @parameter@, @storage@, and @code@.
--
-- For example, @PSC@ would be @parameter@, @storage@ and @code@,
-- @CPS@ would be @code@, @parameter@,and @storage@, and so on.
data EntriesOrder
  = PSC
  | PCS
  | SPC
  | SCP
  | CSP
  | CPS
  deriving stock (EntriesOrder
EntriesOrder -> EntriesOrder -> Bounded EntriesOrder
forall a. a -> a -> Bounded a
maxBound :: EntriesOrder
$cmaxBound :: EntriesOrder
minBound :: EntriesOrder
$cminBound :: EntriesOrder
Bounded, Typeable EntriesOrder
DataType
Constr
Typeable EntriesOrder =>
(forall (c :: * -> *).
 (forall d b. Data d => c (d -> b) -> d -> c b)
 -> (forall g. g -> c g) -> EntriesOrder -> c EntriesOrder)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c EntriesOrder)
-> (EntriesOrder -> Constr)
-> (EntriesOrder -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c EntriesOrder))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c EntriesOrder))
-> ((forall b. Data b => b -> b) -> EntriesOrder -> EntriesOrder)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r)
-> (forall u. (forall d. Data d => d -> u) -> EntriesOrder -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> EntriesOrder -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder)
-> Data EntriesOrder
EntriesOrder -> DataType
EntriesOrder -> Constr
(forall b. Data b => b -> b) -> EntriesOrder -> EntriesOrder
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EntriesOrder -> c EntriesOrder
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EntriesOrder
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) -> EntriesOrder -> u
forall u. (forall d. Data d => d -> u) -> EntriesOrder -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EntriesOrder
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EntriesOrder -> c EntriesOrder
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c EntriesOrder)
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c EntriesOrder)
$cCPS :: Constr
$cCSP :: Constr
$cSCP :: Constr
$cSPC :: Constr
$cPCS :: Constr
$cPSC :: Constr
$tEntriesOrder :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
$cgmapMo :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
gmapMp :: (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
$cgmapMp :: forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
gmapM :: (forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
$cgmapM :: forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> EntriesOrder -> m EntriesOrder
gmapQi :: Int -> (forall d. Data d => d -> u) -> EntriesOrder -> u
$cgmapQi :: forall u. Int -> (forall d. Data d => d -> u) -> EntriesOrder -> u
gmapQ :: (forall d. Data d => d -> u) -> EntriesOrder -> [u]
$cgmapQ :: forall u. (forall d. Data d => d -> u) -> EntriesOrder -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
$cgmapQr :: forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
$cgmapQl :: forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> EntriesOrder -> r
gmapT :: (forall b. Data b => b -> b) -> EntriesOrder -> EntriesOrder
$cgmapT :: (forall b. Data b => b -> b) -> EntriesOrder -> EntriesOrder
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c EntriesOrder)
$cdataCast2 :: forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c EntriesOrder)
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c EntriesOrder)
$cdataCast1 :: forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c EntriesOrder)
dataTypeOf :: EntriesOrder -> DataType
$cdataTypeOf :: EntriesOrder -> DataType
toConstr :: EntriesOrder -> Constr
$ctoConstr :: EntriesOrder -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EntriesOrder
$cgunfold :: forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c EntriesOrder
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EntriesOrder -> c EntriesOrder
$cgfoldl :: forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> EntriesOrder -> c EntriesOrder
$cp1Data :: Typeable EntriesOrder
Data, Int -> EntriesOrder
EntriesOrder -> Int
EntriesOrder -> [EntriesOrder]
EntriesOrder -> EntriesOrder
EntriesOrder -> EntriesOrder -> [EntriesOrder]
EntriesOrder -> EntriesOrder -> EntriesOrder -> [EntriesOrder]
(EntriesOrder -> EntriesOrder)
-> (EntriesOrder -> EntriesOrder)
-> (Int -> EntriesOrder)
-> (EntriesOrder -> Int)
-> (EntriesOrder -> [EntriesOrder])
-> (EntriesOrder -> EntriesOrder -> [EntriesOrder])
-> (EntriesOrder -> EntriesOrder -> [EntriesOrder])
-> (EntriesOrder -> EntriesOrder -> EntriesOrder -> [EntriesOrder])
-> Enum EntriesOrder
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: EntriesOrder -> EntriesOrder -> EntriesOrder -> [EntriesOrder]
$cenumFromThenTo :: EntriesOrder -> EntriesOrder -> EntriesOrder -> [EntriesOrder]
enumFromTo :: EntriesOrder -> EntriesOrder -> [EntriesOrder]
$cenumFromTo :: EntriesOrder -> EntriesOrder -> [EntriesOrder]
enumFromThen :: EntriesOrder -> EntriesOrder -> [EntriesOrder]
$cenumFromThen :: EntriesOrder -> EntriesOrder -> [EntriesOrder]
enumFrom :: EntriesOrder -> [EntriesOrder]
$cenumFrom :: EntriesOrder -> [EntriesOrder]
fromEnum :: EntriesOrder -> Int
$cfromEnum :: EntriesOrder -> Int
toEnum :: Int -> EntriesOrder
$ctoEnum :: Int -> EntriesOrder
pred :: EntriesOrder -> EntriesOrder
$cpred :: EntriesOrder -> EntriesOrder
succ :: EntriesOrder -> EntriesOrder
$csucc :: EntriesOrder -> EntriesOrder
Enum, EntriesOrder -> EntriesOrder -> Bool
(EntriesOrder -> EntriesOrder -> Bool)
-> (EntriesOrder -> EntriesOrder -> Bool) -> Eq EntriesOrder
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: EntriesOrder -> EntriesOrder -> Bool
$c/= :: EntriesOrder -> EntriesOrder -> Bool
== :: EntriesOrder -> EntriesOrder -> Bool
$c== :: EntriesOrder -> EntriesOrder -> Bool
Eq, (forall x. EntriesOrder -> Rep EntriesOrder x)
-> (forall x. Rep EntriesOrder x -> EntriesOrder)
-> Generic EntriesOrder
forall x. Rep EntriesOrder x -> EntriesOrder
forall x. EntriesOrder -> Rep EntriesOrder x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep EntriesOrder x -> EntriesOrder
$cfrom :: forall x. EntriesOrder -> Rep EntriesOrder x
Generic, Int -> EntriesOrder -> ShowS
[EntriesOrder] -> ShowS
EntriesOrder -> String
(Int -> EntriesOrder -> ShowS)
-> (EntriesOrder -> String)
-> ([EntriesOrder] -> ShowS)
-> Show EntriesOrder
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [EntriesOrder] -> ShowS
$cshowList :: [EntriesOrder] -> ShowS
show :: EntriesOrder -> String
$cshow :: EntriesOrder -> String
showsPrec :: Int -> EntriesOrder -> ShowS
$cshowsPrec :: Int -> EntriesOrder -> ShowS
Show)

instance Default EntriesOrder where
  def :: EntriesOrder
def = EntriesOrder
canonicalEntriesOrder
instance NFData EntriesOrder

-- | The canonical entries order which is ordered as follow:
-- @parameter@, @storage@, and @code@.
canonicalEntriesOrder :: EntriesOrder
canonicalEntriesOrder :: EntriesOrder
canonicalEntriesOrder = EntriesOrder
PSC

-- | @(Int, Int, Int)@ is the positions of @parameter@, @storage@, and @code@
-- respectively.
entriesOrderToInt :: EntriesOrder -> (Int, Int, Int)
entriesOrderToInt :: EntriesOrder -> (Int, Int, Int)
entriesOrderToInt = \case
  PSC -> (0, 1, 2)
  PCS -> (0, 2, 1)
  SPC -> (1, 0, 2)
  SCP -> (1, 2, 0)
  CSP -> (2, 1, 0)
  CPS -> (2, 0, 1)

-- | Contract block, convenient when parsing
data ContractBlock op
  = CBParam ParameterType
  | CBStorage Type
  | CBCode [op]
  deriving stock (ContractBlock op -> ContractBlock op -> Bool
(ContractBlock op -> ContractBlock op -> Bool)
-> (ContractBlock op -> ContractBlock op -> Bool)
-> Eq (ContractBlock op)
forall op. Eq op => ContractBlock op -> ContractBlock op -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ContractBlock op -> ContractBlock op -> Bool
$c/= :: forall op. Eq op => ContractBlock op -> ContractBlock op -> Bool
== :: ContractBlock op -> ContractBlock op -> Bool
$c== :: forall op. Eq op => ContractBlock op -> ContractBlock op -> Bool
Eq, Int -> ContractBlock op -> ShowS
[ContractBlock op] -> ShowS
ContractBlock op -> String
(Int -> ContractBlock op -> ShowS)
-> (ContractBlock op -> String)
-> ([ContractBlock op] -> ShowS)
-> Show (ContractBlock op)
forall op. Show op => Int -> ContractBlock op -> ShowS
forall op. Show op => [ContractBlock op] -> ShowS
forall op. Show op => ContractBlock op -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ContractBlock op] -> ShowS
$cshowList :: forall op. Show op => [ContractBlock op] -> ShowS
show :: ContractBlock op -> String
$cshow :: forall op. Show op => ContractBlock op -> String
showsPrec :: Int -> ContractBlock op -> ShowS
$cshowsPrec :: forall op. Show op => Int -> ContractBlock op -> ShowS
Show)

orderContractBlock :: (ContractBlock op, ContractBlock op, ContractBlock op) -> Maybe (Contract' op)
orderContractBlock :: (ContractBlock op, ContractBlock op, ContractBlock op)
-> Maybe (Contract' op)
orderContractBlock = \case
  (CBParam   p :: ParameterType
p, CBStorage s :: Type
s, CBCode    c :: [op]
c) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
PSC
  (CBParam   p :: ParameterType
p, CBCode    c :: [op]
c, CBStorage s :: Type
s) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
PCS
  (CBStorage s :: Type
s, CBParam   p :: ParameterType
p, CBCode    c :: [op]
c) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
SPC
  (CBStorage s :: Type
s, CBCode    c :: [op]
c, CBParam   p :: ParameterType
p) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
SCP
  (CBCode    c :: [op]
c, CBStorage s :: Type
s, CBParam   p :: ParameterType
p) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
CSP
  (CBCode    c :: [op]
c, CBParam   p :: ParameterType
p, CBStorage s :: Type
s) -> Contract' op -> Maybe (Contract' op)
forall a. a -> Maybe a
Just (Contract' op -> Maybe (Contract' op))
-> Contract' op -> Maybe (Contract' op)
forall a b. (a -> b) -> a -> b
$ ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
forall op.
ParameterType -> Type -> [op] -> EntriesOrder -> Contract' op
Contract ParameterType
p Type
s [op]
c EntriesOrder
CPS
  _                                       -> Maybe (Contract' op)
forall a. Maybe a
Nothing

-- | Map each contract fields by the given function and sort the output
-- based on the 'EntriesOrder'.
mapEntriesOrdered
  :: Contract' op
  -> (ParameterType -> a)
  -> (Storage -> a)
  -> ([op] -> a)
  -> [a]
mapEntriesOrdered :: Contract' op
-> (ParameterType -> a) -> (Type -> a) -> ([op] -> a) -> [a]
mapEntriesOrdered Contract{..} fParam :: ParameterType -> a
fParam fStorage :: Type -> a
fStorage fCode :: [op] -> a
fCode =
  ((Int, a) -> a) -> [(Int, a)] -> [a]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Int, a) -> a
forall a b. (a, b) -> b
snd
    ([(Int, a)] -> [a]) -> [(Int, a)] -> [a]
forall a b. (a -> b) -> a -> b
$ ((Int, a) -> Int) -> [(Int, a)] -> [(Int, a)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortWith (Int, a) -> Int
forall a b. (a, b) -> a
fst
        [ (Int
paramPos, ParameterType -> a
fParam ParameterType
contractParameter)
        , (Int
storagePos, Type -> a
fStorage Type
contractStorage)
        , (Int
codePos, [op] -> a
fCode [op]
contractCode)
        ]
  where
    (paramPos :: Int
paramPos, storagePos :: Int
storagePos, codePos :: Int
codePos) = EntriesOrder -> (Int, Int, Int)
entriesOrderToInt EntriesOrder
entriesOrder

type Storage = Type
data Contract' op = Contract
  { Contract' op -> ParameterType
contractParameter :: ParameterType
  , Contract' op -> Type
contractStorage :: Storage
  , Contract' op -> [op]
contractCode :: [op]
  , Contract' op -> EntriesOrder
entriesOrder :: EntriesOrder
  } 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 :: Contract' op
contract =
    RenderContext -> Doc -> Doc
forall a. RenderContext -> a -> a
assertParensNotNeeded RenderContext
pn
      (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ (Element [Doc] -> Doc -> Doc) -> Doc -> [Doc] -> Doc
forall t b. Container t => (Element t -> b -> b) -> b -> t -> b
foldr Element [Doc] -> Doc -> Doc
Doc -> Doc -> Doc
(<$$>) (Text -> Doc
text "")
      ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Contract' op
-> (ParameterType -> Doc)
-> (Type -> Doc)
-> ([op] -> Doc)
-> [Doc]
forall op a.
Contract' op
-> (ParameterType -> a) -> (Type -> a) -> ([op] -> a) -> [a]
mapEntriesOrdered Contract' op
contract
        (\parameter :: ParameterType
parameter -> "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)
        (\storage :: Type
storage -> "storage" Doc -> Doc -> Doc
<+> RenderContext -> Prettier Type -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
needsParens (Type -> Prettier Type
forall a. a -> Prettier a
Prettier Type
storage) Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
semi)
        (\code :: [op]
code -> "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 ''EntriesOrder
deriveJSON morleyAesonOptions ''Contract'