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

{- | Lorentz contracts compilation.

Compilation in one scheme:

@
                    mkContract
                  mkContractWith
  ContractCode  -----------------→  Contract
 (Lorentz code)              (ready compiled contract)
        ↓                                ↑
        ↓                                ↑
defaultContractData            compileLorentzContract
   ContractData                          ↑
        ↓         ContractData           ↑
       (Lorentz code + compilation options)
@

-}
module Lorentz.Run
  ( Contract(..)
  , toMichelsonContract
  , defaultContract

  , CompilationOptions(..)
  , defaultCompilationOptions
  , intactCompilationOptions
  , coBytesTransformerL
  , coOptimizerConfL
  , coStringTransformerL

  , compileLorentz
  , compileLorentzWithOptions

  , mkContract
  , mkContractWith

  , ContractData(..)
  , ContractView(..)
  , defaultContractData
  , compileLorentzContract
  , mkView
  , setViews
  , setViewsRec
  , noViews
  , cdCodeL
  , cdCompilationOptionsL

  , interpretLorentzInstr
  , interpretLorentzLambda

  , analyzeLorentz
  ) where

import Control.Lens.Type as Lens (Lens, Lens')
import Data.Constraint ((\\))
import Data.Default (def)
import Data.Vinyl.Core (Rec(..))
import Data.Vinyl.Functor qualified as Rec
import Data.Vinyl.Recursive qualified as Rec
import Fmt ((+|), (|+))

import Lorentz.Annotation
import Lorentz.Base
import Lorentz.Coercions
import Lorentz.Constraints
import Lorentz.Doc
import Lorentz.Entrypoints
import Lorentz.Entrypoints.Doc
import Lorentz.ViewBase
import Morley.Michelson.Analyzer (AnalyzerRes, analyze)
import Morley.Michelson.Interpret
import Morley.Michelson.Optimizer (OptimizerConf, optimizeWithConf)
import Morley.Michelson.Text (MText)
import Morley.Michelson.Typed (Instr(..), IsoValue, IsoValuesStack(..), ToTs)
import Morley.Michelson.Typed qualified as M
import Morley.Michelson.Typed.Contract (giveNotInView)
import Morley.Michelson.Untyped qualified as U (canonicalEntriesOrder)
import Morley.Util.Lens
import Morley.Util.TypeLits
import Morley.Util.TypeTuple

-- | Options to control Lorentz to Michelson compilation.
data CompilationOptions = CompilationOptions
  { CompilationOptions -> Maybe OptimizerConf
coOptimizerConf :: Maybe OptimizerConf
  -- ^ Config for Michelson optimizer.
  , CompilationOptions -> (Bool, MText -> MText)
coStringTransformer :: (Bool, MText -> MText)
  -- ^ Function to transform strings with. See 'transformStringsLorentz'.
  , CompilationOptions -> (Bool, ByteString -> ByteString)
coBytesTransformer :: (Bool, ByteString -> ByteString)
  -- ^ Function to transform byte strings with. See 'transformBytesLorentz'.
  }

-- | Runs Michelson optimizer with default config and does not touch strings and bytes.
defaultCompilationOptions :: CompilationOptions
defaultCompilationOptions :: CompilationOptions
defaultCompilationOptions = CompilationOptions :: Maybe OptimizerConf
-> (Bool, MText -> MText)
-> (Bool, ByteString -> ByteString)
-> CompilationOptions
CompilationOptions
  { coOptimizerConf :: Maybe OptimizerConf
coOptimizerConf = OptimizerConf -> Maybe OptimizerConf
forall a. a -> Maybe a
Just OptimizerConf
forall a. Default a => a
def
  , coStringTransformer :: (Bool, MText -> MText)
coStringTransformer = (Bool
False, MText -> MText
forall a. a -> a
id)
  , coBytesTransformer :: (Bool, ByteString -> ByteString)
coBytesTransformer = (Bool
False, ByteString -> ByteString
forall a. a -> a
id)
  }

-- | Leave contract without any modifications. For testing purposes.
intactCompilationOptions :: CompilationOptions
intactCompilationOptions :: CompilationOptions
intactCompilationOptions = CompilationOptions :: Maybe OptimizerConf
-> (Bool, MText -> MText)
-> (Bool, ByteString -> ByteString)
-> CompilationOptions
CompilationOptions
  { coOptimizerConf :: Maybe OptimizerConf
coOptimizerConf = Maybe OptimizerConf
forall a. Maybe a
Nothing
  , coStringTransformer :: (Bool, MText -> MText)
coStringTransformer = (Bool
False, MText -> MText
forall a. a -> a
id)
  , coBytesTransformer :: (Bool, ByteString -> ByteString)
coBytesTransformer = (Bool
False, ByteString -> ByteString
forall a. a -> a
id)
  }

-- | For use outside of Lorentz. Will use 'defaultCompilationOptions'.
compileLorentz :: (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentz :: forall (inp :: [*]) (out :: [*]).
(inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentz = CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
forall (inp :: [*]) (out :: [*]).
CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentzWithOptions CompilationOptions
defaultCompilationOptions

-- | Compile Lorentz code, optionally running the optimizer, string and byte transformers.
compileLorentzWithOptions :: CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentzWithOptions :: forall (inp :: [*]) (out :: [*]).
CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentzWithOptions CompilationOptions{Maybe OptimizerConf
(Bool, ByteString -> ByteString)
(Bool, MText -> MText)
coBytesTransformer :: (Bool, ByteString -> ByteString)
coStringTransformer :: (Bool, MText -> MText)
coOptimizerConf :: Maybe OptimizerConf
coBytesTransformer :: CompilationOptions -> (Bool, ByteString -> ByteString)
coStringTransformer :: CompilationOptions -> (Bool, MText -> MText)
coOptimizerConf :: CompilationOptions -> Maybe OptimizerConf
..} =
  (Instr (ToTs inp) (ToTs out) -> Instr (ToTs inp) (ToTs out))
-> (OptimizerConf
    -> Instr (ToTs inp) (ToTs out) -> Instr (ToTs inp) (ToTs out))
-> Maybe OptimizerConf
-> Instr (ToTs inp) (ToTs out)
-> Instr (ToTs inp) (ToTs out)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Instr (ToTs inp) (ToTs out) -> Instr (ToTs inp) (ToTs out)
forall a. a -> a
id OptimizerConf
-> Instr (ToTs inp) (ToTs out) -> Instr (ToTs inp) (ToTs out)
forall (inp :: [T]) (out :: [T]).
OptimizerConf -> Instr inp out -> Instr inp out
optimizeWithConf Maybe OptimizerConf
coOptimizerConf
  (Instr (ToTs inp) (ToTs out) -> Instr (ToTs inp) (ToTs out))
-> ((inp :-> out) -> Instr (ToTs inp) (ToTs out))
-> (inp :-> out)
-> Instr (ToTs inp) (ToTs out)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (inp :-> out) -> Instr (ToTs inp) (ToTs out)
forall (inp :: [*]) (out :: [*]).
(inp :-> out) -> Instr (ToTs inp) (ToTs out)
iAnyCode
  ((inp :-> out) -> Instr (ToTs inp) (ToTs out))
-> ((inp :-> out) -> inp :-> out)
-> (inp :-> out)
-> Instr (ToTs inp) (ToTs out)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool -> (MText -> MText) -> (inp :-> out) -> inp :-> out)
-> (Bool, MText -> MText) -> (inp :-> out) -> inp :-> out
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> (MText -> MText) -> (inp :-> out) -> inp :-> out
forall (inp :: [*]) (out :: [*]).
Bool -> (MText -> MText) -> (inp :-> out) -> inp :-> out
transformStringsLorentz (Bool, MText -> MText)
coStringTransformer
  ((inp :-> out) -> inp :-> out)
-> ((inp :-> out) -> inp :-> out) -> (inp :-> out) -> inp :-> out
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Bool
 -> (ByteString -> ByteString) -> (inp :-> out) -> inp :-> out)
-> (Bool, ByteString -> ByteString) -> (inp :-> out) -> inp :-> out
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Bool -> (ByteString -> ByteString) -> (inp :-> out) -> inp :-> out
forall (inp :: [*]) (out :: [*]).
Bool -> (ByteString -> ByteString) -> (inp :-> out) -> inp :-> out
transformBytesLorentz (Bool, ByteString -> ByteString)
coBytesTransformer

-- | Construct and compile Lorentz contract.
--
-- This is an alias for 'mkContract'.
defaultContract
  :: (NiceParameterFull cp, NiceStorageFull st)
  => (IsNotInView => '[(cp, st)] :-> ContractOut st) -> Contract cp st ()
defaultContract :: forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
(IsNotInView => '[(cp, st)] :-> ContractOut st)
-> Contract cp st ()
defaultContract IsNotInView => '[(cp, st)] :-> ContractOut st
code =
  ContractData cp st () -> Contract cp st ()
forall cp st vd. ContractData cp st vd -> Contract cp st vd
compileLorentzContract (ContractData cp st () -> Contract cp st ())
-> ContractData cp st () -> Contract cp st ()
forall a b. (a -> b) -> a -> b
$ (IsNotInView => '[(cp, st)] :-> ContractOut st)
-> ContractData cp st ()
forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
(IsNotInView => '[(cp, st)] :-> ContractOut st)
-> ContractData cp st ()
defaultContractData IsNotInView => '[(cp, st)] :-> ContractOut st
code

-- | Construct and compile Lorentz contract.
--
-- Note that this accepts code with initial and final stacks unpaired for
-- simplicity.
mkContract
  :: (NiceParameterFull cp, NiceStorageFull st)
  => ContractCode cp st -> Contract cp st ()
mkContract :: forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
ContractCode cp st -> Contract cp st ()
mkContract = CompilationOptions -> ContractCode cp st -> Contract cp st ()
forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
CompilationOptions -> ContractCode cp st -> Contract cp st ()
mkContractWith CompilationOptions
defaultCompilationOptions

-- | Version of 'mkContract' that accepts custom compilation options.
mkContractWith
  :: (NiceParameterFull cp, NiceStorageFull st)
  => CompilationOptions -> ContractCode cp st -> Contract cp st ()
mkContractWith :: forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
CompilationOptions -> ContractCode cp st -> Contract cp st ()
mkContractWith CompilationOptions
opts ContractCode cp st
code =
  ContractData cp st () -> Contract cp st ()
forall cp st vd. ContractData cp st vd -> Contract cp st vd
compileLorentzContract (ContractData cp st () -> Contract cp st ())
-> ContractData cp st () -> Contract cp st ()
forall a b. (a -> b) -> a -> b
$ ContractCode cp st
-> Rec (ContractView st) (RevealViews ())
-> CompilationOptions
-> ContractData cp st ()
forall cp st vd.
(NiceParameterFull cp, NiceStorageFull st,
 NiceViewsDescriptor vd) =>
ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
ContractData ContractCode cp st
code Rec (ContractView st) (RevealViews ())
forall a. Monoid a => a
mempty CompilationOptions
opts

-- | Code for a contract along with compilation options for the Lorentz compiler.
--
-- It is expected that a 'Contract' is one packaged entity, wholly controlled by its author.
-- Therefore the author should be able to set all options that control contract's behavior.
--
-- This helps ensure that a given contract will be interpreted in the same way in all
-- environments, like production and testing.
--
-- Raw 'ContractCode' should not be used for distribution of contracts.
data ContractData cp st vd =
  (NiceParameterFull cp, NiceStorageFull st, NiceViewsDescriptor vd) =>
  ContractData
  { forall cp st vd. ContractData cp st vd -> ContractCode cp st
cdCode :: ContractCode cp st
  -- ^ The contract itself.
  , forall cp st vd.
ContractData cp st vd -> Rec (ContractView st) (RevealViews vd)
cdViews :: Rec (ContractView st) (RevealViews vd)
  -- ^ Contract views.
  , forall cp st vd. ContractData cp st vd -> CompilationOptions
cdCompilationOptions :: CompilationOptions
  -- ^ General compilation options for the Lorentz compiler.
  }

-- | Single contract view.
data ContractView st (v :: ViewTyInfo) where
  ContractView
    :: ( KnownSymbol name, NiceViewable arg, NiceViewable ret
       , HasAnnotation arg, HasAnnotation ret
       )
    => ViewCode arg st ret
    -> ContractView st ('ViewTyInfo name arg ret)

-- | Construct a view.
--
-- > mkView @"add" @(Integer, Integer) do
-- >  car; unpair; add
mkView
  :: forall name arg ret st.
     ( KnownSymbol name, NiceViewable arg, NiceViewable ret
     , HasAnnotation arg, HasAnnotation ret
     , TypeHasDoc arg, TypeHasDoc ret
     )
  => ViewCode arg st ret
  -> ContractView st ('ViewTyInfo name arg ret)
mkView :: forall (name :: Symbol) arg ret st.
(KnownSymbol name, NiceViewable arg, NiceViewable ret,
 HasAnnotation arg, HasAnnotation ret, TypeHasDoc arg,
 TypeHasDoc ret) =>
ViewCode arg st ret -> ContractView st ('ViewTyInfo name arg ret)
mkView ViewCode arg st ret
code = ViewCode arg st ret -> ContractView st ('ViewTyInfo name arg ret)
forall (name :: Symbol) arg ret st.
(KnownSymbol name, NiceViewable arg, NiceViewable ret,
 HasAnnotation arg, HasAnnotation ret) =>
ViewCode arg st ret -> ContractView st ('ViewTyInfo name arg ret)
ContractView (ViewCode arg st ret -> ContractView st ('ViewTyInfo name arg ret))
-> ViewCode arg st ret
-> ContractView st ('ViewTyInfo name arg ret)
forall a b. (a -> b) -> a -> b
$
  (SubDoc -> DView) -> ViewCode arg st ret -> ViewCode arg st ret
forall di (inp :: [*]) (out :: [*]).
DocItem di =>
(SubDoc -> di) -> (inp :-> out) -> inp :-> out
docGroup (ViewName -> SubDoc -> DView
DView (forall (name :: Symbol).
(KnownSymbol name, HasCallStack) =>
ViewName
demoteViewName @name)) (ViewCode arg st ret -> ViewCode arg st ret)
-> ViewCode arg st ret -> ViewCode arg st ret
forall a b. (a -> b) -> a -> b
$
    DViewArg -> '[(arg, st)] :-> '[(arg, st)]
forall di (s :: [*]). DocItem di => di -> s :-> s
doc (Proxy arg -> DViewArg
forall a. (NiceViewable a, TypeHasDoc a) => Proxy a -> DViewArg
DViewArg (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @arg)) ('[(arg, st)] :-> '[(arg, st)])
-> ('[(arg, st)] :-> '[(arg, st)]) -> '[(arg, st)] :-> '[(arg, st)]
forall (a :: [*]) (b :: [*]) (c :: [*]).
(a :-> b) -> (b :-> c) -> a :-> c
#
    DViewRet -> '[(arg, st)] :-> '[(arg, st)]
forall di (s :: [*]). DocItem di => di -> s :-> s
doc (Proxy ret -> DViewRet
forall a. (NiceViewable a, TypeHasDoc a) => Proxy a -> DViewRet
DViewRet (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @ret)) ('[(arg, st)] :-> '[(arg, st)])
-> ViewCode arg st ret -> ViewCode arg st ret
forall (a :: [*]) (b :: [*]) (c :: [*]).
(a :-> b) -> (b :-> c) -> a :-> c
#
    ViewCode arg st ret
code

-- | Compile contract with 'defaultCompilationOptions'.
defaultContractData
  :: forall cp st. (NiceParameterFull cp, NiceStorageFull st)
  => (IsNotInView => '[(cp, st)] :-> ContractOut st) -> ContractData cp st ()
defaultContractData :: forall cp st.
(NiceParameterFull cp, NiceStorageFull st) =>
(IsNotInView => '[(cp, st)] :-> ContractOut st)
-> ContractData cp st ()
defaultContractData IsNotInView => '[(cp, st)] :-> ContractOut st
code = ContractData :: forall cp st vd.
(NiceParameterFull cp, NiceStorageFull st,
 NiceViewsDescriptor vd) =>
ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
ContractData
  { cdCode :: ContractCode cp st
cdCode = (IsNotInView => '[(cp, st)] :-> ContractOut st)
-> ContractCode cp st
forall cp st.
(IsNotInView => '[(cp, st)] :-> ContractOut st)
-> ContractCode cp st
mkContractCode IsNotInView => '[(cp, st)] :-> ContractOut st
code
  , cdViews :: Rec (ContractView st) (RevealViews ())
cdViews = Rec (ContractView st) (RevealViews ())
forall {u} (a :: u -> *). Rec a '[]
RNil
  , cdCompilationOptions :: CompilationOptions
cdCompilationOptions = CompilationOptions
defaultCompilationOptions
  }

-- | Compile a whole contract to Michelson.
--
-- Note that compiled contract can be ill-typed in terms of Michelson code
-- when some of the compilation options are used.
-- However, compilation with 'defaultCompilationOptions' should be valid.
compileLorentzContract
  :: forall cp st vd. ContractData cp st vd -> Contract cp st vd
compileLorentzContract :: forall cp st vd. ContractData cp st vd -> Contract cp st vd
compileLorentzContract ContractData{Rec (ContractView st) (RevealViews vd)
ContractCode cp st
CompilationOptions
cdCompilationOptions :: CompilationOptions
cdViews :: Rec (ContractView st) (RevealViews vd)
cdCode :: ContractCode cp st
cdCompilationOptions :: forall cp st vd. ContractData cp st vd -> CompilationOptions
cdViews :: forall cp st vd.
ContractData cp st vd -> Rec (ContractView st) (RevealViews vd)
cdCode :: forall cp st vd. ContractData cp st vd -> ContractCode cp st
..} = Contract :: forall cp st vd.
(NiceParameter cp, NiceStorage st, NiceViewsDescriptor vd) =>
Contract (ToT cp) (ToT st)
-> ContractCode cp st -> Contract cp st vd
Contract{Contract (ToT cp) (ToT st)
ContractCode cp st
cDocumentedCode :: ContractCode cp st
cMichelsonContract :: Contract (ToT cp) (ToT st)
cDocumentedCode :: ContractCode cp st
cMichelsonContract :: Contract (ToT cp) (ToT st)
..}
  where
    cMichelsonContract :: Contract (ToT cp) (ToT st)
cMichelsonContract = Contract :: forall (instr :: [T] -> [T] -> *) (cp :: T) (st :: T).
(ParameterScope cp, StorageScope st) =>
ContractCode' instr cp st
-> ParamNotes cp
-> Notes st
-> ViewsSet' instr st
-> EntriesOrder
-> Contract' instr cp st
M.Contract
      { cCode :: ContractCode' Instr (ToT cp) (ToT st)
cCode = Instr (ContractInp (ToT cp) (ToT st)) (ContractOut (ToT st))
-> ContractCode' Instr (ToT cp) (ToT st)
forall (instr :: [T] -> [T] -> *) (cp :: T) (st :: T).
instr (ContractInp cp st) (ContractOut st)
-> ContractCode' instr cp st
M.ContractCode (Instr (ContractInp (ToT cp) (ToT st)) (ContractOut (ToT st))
 -> ContractCode' Instr (ToT cp) (ToT st))
-> Instr (ContractInp (ToT cp) (ToT st)) (ContractOut (ToT st))
-> ContractCode' Instr (ToT cp) (ToT st)
forall a b. (a -> b) -> a -> b
$
          CompilationOptions
-> ('[(cp, st)] :-> ContractOut st)
-> Instr (ToTs '[(cp, st)]) (ToTs (ContractOut st))
forall (inp :: [*]) (out :: [*]).
CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentzWithOptions CompilationOptions
cdCompilationOptions (('[(cp, st)] :-> ContractOut st)
 -> Instr (ToTs '[(cp, st)]) (ToTs (ContractOut st)))
-> ('[(cp, st)] :-> ContractOut st)
-> Instr (ToTs '[(cp, st)]) (ToTs (ContractOut st))
forall a b. (a -> b) -> a -> b
$ ContractCode cp st -> '[(cp, st)] :-> ContractOut st
forall cp st. ContractCode cp st -> '[(cp, st)] :-> ContractOut st
unContractCode ContractCode cp st
cdCode
      , cParamNotes :: ParamNotes (ToT cp)
cParamNotes = ParamNotes (ToT cp)
cpNotes
      , cStoreNotes :: Notes (ToT st)
cStoreNotes = forall a. HasAnnotation a => FollowEntrypointFlag -> Notes (ToT a)
getAnnotation @st FollowEntrypointFlag
NotFollowEntrypoint
      , cEntriesOrder :: EntriesOrder
cEntriesOrder = EntriesOrder
U.canonicalEntriesOrder
      , cViews :: ViewsSet' Instr (ToT st)
cViews = CompilationOptions
-> Rec (ContractView st) (RevealViews vd)
-> ViewsSet' Instr (ToT st)
forall (vs :: [ViewTyInfo]) st.
(HasCallStack, KnownValue st) =>
CompilationOptions -> Rec (ContractView st) vs -> ViewsSet (ToT st)
compileLorentzViews CompilationOptions
cdCompilationOptions Rec (ContractView st) (RevealViews vd)
cdViews
      } (ParameterScope (ToT cp) => Contract (ToT cp) (ToT st))
-> (NiceParameter cp :- ParameterScope (ToT cp))
-> Contract (ToT cp) (ToT st)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ forall a. NiceParameter a :- ParameterScope (ToT a)
niceParameterEvi @cp
        (StorageScope (ToT st) => Contract (ToT cp) (ToT st))
-> (((SingI (ToT st), WellTyped (ToT st),
      FailOnOperationFound (ContainsOp (ToT st)),
      FailOnNestedBigMapsFound (ContainsNestedBigMaps (ToT st)),
      FailOnContractFound (ContainsContract (ToT st))),
     KnownValue st)
    :- StorageScope (ToT st))
-> Contract (ToT cp) (ToT st)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ forall a. NiceStorage a :- StorageScope (ToT a)
niceStorageEvi @st

    cDocumentedCode :: ContractCode cp st
cDocumentedCode = case ContractCode cp st
cdCode of
      ContractCode '[(cp, st)] :-> ContractOut st
x -> ('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
forall cp st.
('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
ContractCode (('[(cp, st)] :-> ContractOut st) -> ContractCode cp st)
-> ('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
forall a b. (a -> b) -> a -> b
$
        Proxy cp
-> ('[(cp, st)] :-> ContractOut st)
-> '[(cp, st)] :-> ContractOut st
forall cp (inp :: [*]) (out :: [*]).
(NiceParameterFull cp, HasCallStack) =>
Proxy cp -> (inp :-> out) -> inp :-> out
finalizeParamCallingDoc' (forall {t}. Proxy t
forall {k} (t :: k). Proxy t
Proxy @cp) '[(cp, st)] :-> ContractOut st
x ('[(cp, st)] :-> ContractOut st)
-> (ContractOut st :-> ContractOut st)
-> '[(cp, st)] :-> ContractOut st
forall (a :: [*]) (b :: [*]) (c :: [*]).
(a :-> b) -> (b :-> c) -> a :-> c
#
        (Element [ContractOut st :-> ContractOut st]
 -> (ContractOut st :-> ContractOut st)
 -> ContractOut st :-> ContractOut st)
-> (ContractOut st :-> ContractOut st)
-> [ContractOut st :-> ContractOut st]
-> ContractOut st :-> ContractOut st
forall t b. Container t => (Element t -> b -> b) -> b -> t -> b
foldr Element [ContractOut st :-> ContractOut st]
-> (ContractOut st :-> ContractOut st)
-> ContractOut st :-> ContractOut st
forall (a :: [*]) (b :: [*]) (c :: [*]).
(a :-> b) -> (b :-> c) -> a :-> c
(#) (Instr (ToTs (ContractOut st)) (ToTs (ContractOut st))
-> ContractOut st :-> ContractOut st
forall (inp :: [*]) (out :: [*]).
Instr (ToTs inp) (ToTs out) -> inp :-> out
I Instr (ToTs (ContractOut st)) (ToTs (ContractOut st))
forall (inp :: [T]). Instr inp inp
Nop)
          ( Rec (Const (ContractOut st :-> ContractOut st)) (RevealViews vd)
-> [ContractOut st :-> ContractOut st]
forall {u} a (rs :: [u]). Rec (Const a) rs -> [a]
Rec.recordToList (Rec (Const (ContractOut st :-> ContractOut st)) (RevealViews vd)
 -> [ContractOut st :-> ContractOut st])
-> Rec (Const (ContractOut st :-> ContractOut st)) (RevealViews vd)
-> [ContractOut st :-> ContractOut st]
forall a b. (a -> b) -> a -> b
$
            (forall (x :: ViewTyInfo).
 ContractView st x -> Const (ContractOut st :-> ContractOut st) x)
-> Rec (ContractView st) (RevealViews vd)
-> Rec (Const (ContractOut st :-> ContractOut st)) (RevealViews vd)
forall {u} (f :: u -> *) (g :: u -> *) (rs :: [u]).
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
Rec.rmap (\(ContractView ViewCode arg st ret
code) -> (ContractOut st :-> ContractOut st)
-> Const (ContractOut st :-> ContractOut st) x
forall k a (b :: k). a -> Const a b
Rec.Const ((ContractOut st :-> ContractOut st)
 -> Const (ContractOut st :-> ContractOut st) x)
-> (ContractOut st :-> ContractOut st)
-> Const (ContractOut st :-> ContractOut st) x
forall a b. (a -> b) -> a -> b
$ ViewCode arg st ret -> ContractOut st :-> ContractOut st
forall (s1 :: [*]) (s2 :: [*]) (s1' :: [*]) (s2' :: [*]).
(s1 :-> s2) -> s1' :-> s2'
fakeCoercing ViewCode arg st ret
code) Rec (ContractView st) (RevealViews vd)
cdViews
          )

    cpNotes :: ParamNotes (ToT cp)
cpNotes = forall cp. ParameterDeclaresEntrypoints cp => ParamNotes (ToT cp)
parameterEntrypointsToNotes @cp

-- | Compile multiple views, with the related checks.
compileLorentzViews
  :: forall vs st.
      ( HasCallStack
      , KnownValue st
      )
  => CompilationOptions
  -> Rec (ContractView st) vs
  -> M.ViewsSet (M.ToT st)
compileLorentzViews :: forall (vs :: [ViewTyInfo]) st.
(HasCallStack, KnownValue st) =>
CompilationOptions -> Rec (ContractView st) vs -> ViewsSet (ToT st)
compileLorentzViews CompilationOptions
co Rec (ContractView st) vs
views =
  let
    viewsList :: [SomeView' Instr (ToT st)]
viewsList =
      Rec (Const (SomeView' Instr (ToT st))) vs
-> [SomeView' Instr (ToT st)]
forall {u} a (rs :: [u]). Rec (Const a) rs -> [a]
Rec.recordToList (Rec (Const (SomeView' Instr (ToT st))) vs
 -> [SomeView' Instr (ToT st)])
-> Rec (Const (SomeView' Instr (ToT st))) vs
-> [SomeView' Instr (ToT st)]
forall a b. (a -> b) -> a -> b
$
      (forall (x :: ViewTyInfo).
 ContractView st x -> Const (SomeView' Instr (ToT st)) x)
-> Rec (ContractView st) vs
-> Rec (Const (SomeView' Instr (ToT st))) vs
forall {u} (f :: u -> *) (g :: u -> *) (rs :: [u]).
(forall (x :: u). f x -> g x) -> Rec f rs -> Rec g rs
Rec.rmap (\ContractView st x
v -> SomeView' Instr (ToT st) -> Const (SomeView' Instr (ToT st)) x
forall k a (b :: k). a -> Const a b
Rec.Const (SomeView' Instr (ToT st) -> Const (SomeView' Instr (ToT st)) x)
-> SomeView' Instr (ToT st) -> Const (SomeView' Instr (ToT st)) x
forall a b. (a -> b) -> a -> b
$ CompilationOptions -> ContractView st x -> SomeView' Instr (ToT st)
forall st (vt :: ViewTyInfo).
KnownValue st =>
CompilationOptions -> ContractView st vt -> SomeView (ToT st)
compileLorentzView CompilationOptions
co ContractView st x
v) Rec (ContractView st) vs
views
  in case [SomeView' Instr (ToT st)]
-> Either ViewsSetError (ViewsSet (ToT st))
forall (instr :: [T] -> [T] -> *) (st :: T).
[SomeView' instr st] -> Either ViewsSetError (ViewsSet' instr st)
M.mkViewsSet [SomeView' Instr (ToT st)]
viewsList of
      Right ViewsSet (ToT st)
viewsSet -> ViewsSet (ToT st)
viewsSet
      Left e :: ViewsSetError
e@M.DuplicatedViewName{} ->
        Text -> ViewsSet (ToT st)
forall a. HasCallStack => Text -> a
error (Text -> ViewsSet (ToT st)) -> Text -> ViewsSet (ToT st)
forall a b. (a -> b) -> a -> b
$ Builder
"An impossible happened: " Builder -> Builder -> Text
forall b. FromBuilder b => Builder -> Builder -> b
+| ViewsSetError
e ViewsSetError -> Builder -> Builder
forall a b. (Buildable a, FromBuilder b) => a -> Builder -> b
|+ Builder
""

-- | Compile a single view.
compileLorentzView
  :: forall st vt.
     (KnownValue st)
  => CompilationOptions
  -> ContractView st vt
  -> M.SomeView (M.ToT st)
compileLorentzView :: forall st (vt :: ViewTyInfo).
KnownValue st =>
CompilationOptions -> ContractView st vt -> SomeView (ToT st)
compileLorentzView CompilationOptions
co (ContractView ViewCode arg st ret
viewCode)
  | (Proxy ('ViewTyInfo name arg ret)
_ :: Proxy ('ViewTyInfo name arg ret)) <- forall {k} (t :: k). Proxy t
forall {t :: ViewTyInfo}. Proxy t
Proxy @vt =
      View' Instr (ToT arg) (ToT st) (ToT ret)
-> SomeView' Instr (ToT st)
forall (instr :: [T] -> [T] -> *) (arg :: T) (st :: T) (ret :: T).
View' instr arg st ret -> SomeView' instr st
M.SomeView View :: forall (instr :: [T] -> [T] -> *) (arg :: T) (st :: T) (ret :: T).
(ViewableScope arg, SingI st, ViewableScope ret) =>
ViewName
-> Notes arg
-> Notes ret
-> ViewCode' instr arg st ret
-> View' instr arg st ret
M.View
        { vName :: ViewName
M.vName = forall (name :: Symbol).
(KnownSymbol name, HasCallStack) =>
ViewName
demoteViewName @name
        , vArgument :: Notes (ToT arg)
M.vArgument = forall a. HasAnnotation a => FollowEntrypointFlag -> Notes (ToT a)
getAnnotation @arg FollowEntrypointFlag
NotFollowEntrypoint
        , vReturn :: Notes (ToT ret)
M.vReturn = forall a. HasAnnotation a => FollowEntrypointFlag -> Notes (ToT a)
getAnnotation @ret FollowEntrypointFlag
NotFollowEntrypoint
        , vCode :: ViewCode' Instr (ToT arg) (ToT st) (ToT ret)
M.vCode = CompilationOptions
-> ViewCode arg st ret -> Instr (ToTs '[(arg, st)]) (ToTs '[ret])
forall (inp :: [*]) (out :: [*]).
CompilationOptions -> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentzWithOptions CompilationOptions
co ViewCode arg st ret
viewCode
        }
        (ViewableScope (ToT arg) => SomeView' Instr (ToT st))
-> (NiceViewable arg :- ViewableScope (ToT arg))
-> SomeView' Instr (ToT st)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ forall a. NiceViewable a :- ViewableScope (ToT a)
niceViewableEvi @arg
        (ViewableScope (ToT ret) => SomeView' Instr (ToT st))
-> (NiceViewable ret :- ViewableScope (ToT ret))
-> SomeView' Instr (ToT st)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ forall a. NiceViewable a :- ViewableScope (ToT a)
niceViewableEvi @ret

{- | Set all the contract's views.

@
compileLorentzContract $
  defaultContractData do
    ...
  & setViews
    ( mkView @"myView" @() do
        ...
    , mkView @"anotherView" @Integer do
        ...
    )
@
-}
setViews
  :: forall vd cp st.
     ( RecFromTuple (Rec (ContractView st) (RevealViews vd))
     , NiceViewsDescriptor vd
     )
  => IsoRecTuple (Rec (ContractView st) (RevealViews vd))
  -> ContractData cp st ()
  -> ContractData cp st vd
setViews :: forall vd cp st.
(RecFromTuple (Rec (ContractView st) (RevealViews vd)),
 NiceViewsDescriptor vd) =>
IsoRecTuple (Rec (ContractView st) (RevealViews vd))
-> ContractData cp st () -> ContractData cp st vd
setViews IsoRecTuple (Rec (ContractView st) (RevealViews vd))
views = Rec (ContractView st) (RevealViews vd)
-> ContractData cp st () -> ContractData cp st vd
forall vd cp st.
NiceViewsDescriptor vd =>
Rec (ContractView st) (RevealViews vd)
-> ContractData cp st () -> ContractData cp st vd
setViewsRec (IsoRecTuple (Rec (ContractView st) (RevealViews vd))
-> Rec (ContractView st) (RevealViews vd)
forall r. RecFromTuple r => IsoRecTuple r -> r
recFromTuple IsoRecTuple (Rec (ContractView st) (RevealViews vd))
views)

-- | Version of 'setViews' that accepts a 'Rec'.
--
-- May be useful if you have too many views or want to combine views sets.
setViewsRec
  :: forall vd cp st.
     (NiceViewsDescriptor vd)
  => Rec (ContractView st) (RevealViews vd)
  -> ContractData cp st ()
  -> ContractData cp st vd
setViewsRec :: forall vd cp st.
NiceViewsDescriptor vd =>
Rec (ContractView st) (RevealViews vd)
-> ContractData cp st () -> ContractData cp st vd
setViewsRec Rec (ContractView st) (RevealViews vd)
views ContractData{Rec (ContractView st) (RevealViews ())
ContractCode cp st
CompilationOptions
cdCompilationOptions :: CompilationOptions
cdViews :: Rec (ContractView st) (RevealViews ())
cdCode :: ContractCode cp st
cdCompilationOptions :: forall cp st vd. ContractData cp st vd -> CompilationOptions
cdViews :: forall cp st vd.
ContractData cp st vd -> Rec (ContractView st) (RevealViews vd)
cdCode :: forall cp st vd. ContractData cp st vd -> ContractCode cp st
..} =
  ContractData :: forall cp st vd.
(NiceParameterFull cp, NiceStorageFull st,
 NiceViewsDescriptor vd) =>
ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
ContractData{ cdViews :: Rec (ContractView st) (RevealViews vd)
cdViews = Rec (ContractView st) (RevealViews vd)
views, ContractCode cp st
CompilationOptions
cdCompilationOptions :: CompilationOptions
cdCode :: ContractCode cp st
cdCompilationOptions :: CompilationOptions
cdCode :: ContractCode cp st
.. }

-- | Restrict type of 'Contract', 'ContractData' or other similar type to
-- have no views.
noViews :: contract cp st () -> contract cp st ()
noViews :: forall {k} {k} (contract :: k -> k -> * -> *) (cp :: k) (st :: k).
contract cp st () -> contract cp st ()
noViews = contract cp st () -> contract cp st ()
forall a. a -> a
id

-- | Interpret a Lorentz instruction, for test purposes. Note that this does not run the
-- optimizer.
interpretLorentzInstr
  :: (IsoValuesStack inp, IsoValuesStack out)
  => ContractEnv
  -> (IsNotInView => inp :-> out)
  -> Rec Identity inp
  -> Either MichelsonFailureWithStack (Rec Identity out)
interpretLorentzInstr :: forall (inp :: [*]) (out :: [*]).
(IsoValuesStack inp, IsoValuesStack out) =>
ContractEnv
-> (IsNotInView => inp :-> out)
-> Rec Identity inp
-> Either MichelsonFailureWithStack (Rec Identity out)
interpretLorentzInstr ContractEnv
env IsNotInView => inp :-> out
instr Rec Identity inp
inp =
  Rec Value (ToTs out) -> Rec Identity out
forall (ts :: [*]).
IsoValuesStack ts =>
Rec Value (ToTs ts) -> Rec Identity ts
fromValStack (Rec Value (ToTs out) -> Rec Identity out)
-> Either MichelsonFailureWithStack (Rec Value (ToTs out))
-> Either MichelsonFailureWithStack (Rec Identity out)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ContractEnv
-> Instr (ToTs inp) (ToTs out)
-> Rec Value (ToTs inp)
-> Either MichelsonFailureWithStack (Rec Value (ToTs out))
forall (inp :: [T]) (out :: [T]).
ContractEnv
-> Instr inp out
-> Rec Value inp
-> Either MichelsonFailureWithStack (Rec Value out)
interpretInstr ContractEnv
env ((inp :-> out) -> Instr (ToTs inp) (ToTs out)
forall (inp :: [*]) (out :: [*]).
(inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentz ((inp :-> out) -> Instr (ToTs inp) (ToTs out))
-> (inp :-> out) -> Instr (ToTs inp) (ToTs out)
forall a b. (a -> b) -> a -> b
$ (IsNotInView => inp :-> out) -> inp :-> out
forall r. (IsNotInView => r) -> r
giveNotInView IsNotInView => inp :-> out
instr) (Rec Identity inp -> Rec Value (ToTs inp)
forall (ts :: [*]).
IsoValuesStack ts =>
Rec Identity ts -> Rec Value (ToTs ts)
toValStack Rec Identity inp
inp)

-- | Like 'interpretLorentzInstr', but works on singleton input and output
-- stacks.
interpretLorentzLambda
  :: (IsoValue inp, IsoValue out)
  => ContractEnv
  -> (IsNotInView => Fn inp out)
  -> inp
  -> Either MichelsonFailureWithStack out
interpretLorentzLambda :: forall inp out.
(IsoValue inp, IsoValue out) =>
ContractEnv
-> (IsNotInView => Fn inp out)
-> inp
-> Either MichelsonFailureWithStack out
interpretLorentzLambda ContractEnv
env IsNotInView => Fn inp out
instr inp
inp = do
  Rec Identity '[out]
res <- ContractEnv
-> (IsNotInView => Fn inp out)
-> Rec Identity '[inp]
-> Either MichelsonFailureWithStack (Rec Identity '[out])
forall (inp :: [*]) (out :: [*]).
(IsoValuesStack inp, IsoValuesStack out) =>
ContractEnv
-> (IsNotInView => inp :-> out)
-> Rec Identity inp
-> Either MichelsonFailureWithStack (Rec Identity out)
interpretLorentzInstr ContractEnv
env IsNotInView => Fn inp out
instr (inp -> Identity inp
forall a. a -> Identity a
Identity inp
inp Identity inp -> Rec Identity '[] -> Rec Identity '[inp]
forall {u} (a :: u -> *) (r :: u) (rs :: [u]).
a r -> Rec a rs -> Rec a (r : rs)
:& Rec Identity '[]
forall {u} (a :: u -> *). Rec a '[]
RNil)
  let Identity out
r
out :& Rec Identity rs
RNil = Rec Identity '[out]
res
  out -> Either MichelsonFailureWithStack out
forall (m :: * -> *) a. Monad m => a -> m a
return out
out

instance ContainsDoc (ContractData cp st vd) where
  buildDocUnfinalized :: ContractData cp st vd -> ContractDoc
buildDocUnfinalized =
    Contract cp st vd -> ContractDoc
forall a. ContainsDoc a => a -> ContractDoc
buildDocUnfinalized (Contract cp st vd -> ContractDoc)
-> (ContractData cp st vd -> Contract cp st vd)
-> ContractData cp st vd
-> ContractDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractData cp st vd -> Contract cp st vd
forall cp st vd. ContractData cp st vd -> Contract cp st vd
compileLorentzContract
instance ContainsUpdateableDoc (ContractData cp st vd) where
  modifyDocEntirely :: (SomeDocItem -> SomeDocItem)
-> ContractData cp st vd -> ContractData cp st vd
modifyDocEntirely SomeDocItem -> SomeDocItem
how ContractData cp st vd
c =
    ContractData cp st vd
c{ cdCode :: ContractCode cp st
cdCode = case ContractData cp st vd -> ContractCode cp st
forall cp st vd. ContractData cp st vd -> ContractCode cp st
cdCode ContractData cp st vd
c of
        ContractCode '[(cp, st)] :-> ContractOut st
x -> ('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
forall cp st.
('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
ContractCode (('[(cp, st)] :-> ContractOut st) -> ContractCode cp st)
-> ('[(cp, st)] :-> ContractOut st) -> ContractCode cp st
forall a b. (a -> b) -> a -> b
$ (SomeDocItem -> SomeDocItem)
-> ('[(cp, st)] :-> ContractOut st)
-> '[(cp, st)] :-> ContractOut st
forall a.
ContainsUpdateableDoc a =>
(SomeDocItem -> SomeDocItem) -> a -> a
modifyDocEntirely SomeDocItem -> SomeDocItem
how '[(cp, st)] :-> ContractOut st
x }

-- | Lorentz version of analyzer.
analyzeLorentz :: inp :-> out -> AnalyzerRes
analyzeLorentz :: forall (inp :: [*]) (out :: [*]). (inp :-> out) -> AnalyzerRes
analyzeLorentz = Instr (ToTs inp) (ToTs out) -> AnalyzerRes
forall (inp :: [T]) (out :: [T]). Instr inp out -> AnalyzerRes
analyze (Instr (ToTs inp) (ToTs out) -> AnalyzerRes)
-> ((inp :-> out) -> Instr (ToTs inp) (ToTs out))
-> (inp :-> out)
-> AnalyzerRes
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (inp :-> out) -> Instr (ToTs inp) (ToTs out)
forall (inp :: [*]) (out :: [*]).
(inp :-> out) -> Instr (ToTs inp) (ToTs out)
compileLorentz

makeLensesWith postfixLFields ''CompilationOptions

cdCodeL ::
  forall cp st vd cp1. NiceParameterFull cp1 =>
  Lens.Lens (ContractData cp st vd) (ContractData cp1 st vd) (ContractCode cp st) (ContractCode cp1 st)
cdCodeL :: forall cp st vd cp1.
NiceParameterFull cp1 =>
Lens
  (ContractData cp st vd)
  (ContractData cp1 st vd)
  (ContractCode cp st)
  (ContractCode cp1 st)
cdCodeL ContractCode cp st -> f (ContractCode cp1 st)
f (ContractData ContractCode cp st
code Rec (ContractView st) (RevealViews vd)
views CompilationOptions
options)
  = (ContractCode cp1 st -> ContractData cp1 st vd)
-> f (ContractCode cp1 st) -> f (ContractData cp1 st vd)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\ContractCode cp1 st
code' -> ContractCode cp1 st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp1 st vd
forall cp st vd.
(NiceParameterFull cp, NiceStorageFull st,
 NiceViewsDescriptor vd) =>
ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
ContractData ContractCode cp1 st
code' Rec (ContractView st) (RevealViews vd)
views CompilationOptions
options) (ContractCode cp st -> f (ContractCode cp1 st)
f ContractCode cp st
code)

cdCompilationOptionsL ::
  forall cp st vd.
  Lens.Lens' (ContractData cp st vd) CompilationOptions
cdCompilationOptionsL :: forall cp st vd. Lens' (ContractData cp st vd) CompilationOptions
cdCompilationOptionsL CompilationOptions -> f CompilationOptions
f (ContractData ContractCode cp st
code Rec (ContractView st) (RevealViews vd)
views CompilationOptions
options)
  = (CompilationOptions -> ContractData cp st vd)
-> f CompilationOptions -> f (ContractData cp st vd)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\CompilationOptions
options' -> ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
forall cp st vd.
(NiceParameterFull cp, NiceStorageFull st,
 NiceViewsDescriptor vd) =>
ContractCode cp st
-> Rec (ContractView st) (RevealViews vd)
-> CompilationOptions
-> ContractData cp st vd
ContractData ContractCode cp st
code Rec (ContractView st) (RevealViews vd)
views CompilationOptions
options') (CompilationOptions -> f CompilationOptions
f CompilationOptions
options)