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

-- | Operation size evaluation.
module Lorentz.OpSize
  ( OpSize (..)
  , T.opSizeHardLimit
  , T.smallTransferOpSize

  , contractOpSize
  , valueOpSize
  ) where

import Data.Constraint ((\\))

import Lorentz.Constraints
import Lorentz.Run
import Lorentz.Value
import Michelson.Typed (OpSize)
import qualified Michelson.Typed as T

-- | Estimate code operation size.
contractOpSize
  :: (NiceParameterFull cp, NiceStorage st)
  => Contract cp st -> OpSize
contractOpSize :: Contract cp st -> OpSize
contractOpSize =
  Contract (ToT cp) (ToT st) -> OpSize
forall (cp :: T) (st :: T). Contract cp st -> OpSize
T.contractOpSize (Contract (ToT cp) (ToT st) -> OpSize)
-> (Contract cp st -> Contract (ToT cp) (ToT st))
-> Contract cp st
-> OpSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Contract cp st -> Contract (ToT cp) (ToT st)
forall cp st.
(NiceParameterFull cp, NiceStorage st) =>
Contract cp st -> Contract (ToT cp) (ToT st)
compileLorentzContract

{- We do not provide a method for plain lorentz code because it can be
   compiled differently (e.g. with optimizations or not).
-}

-- | Estimate value operation size.
valueOpSize :: forall a. (NicePrintedValue a) => a -> OpSize
valueOpSize :: a -> OpSize
valueOpSize =
  Value (ToT a) -> OpSize
forall (t :: T). PrintedValScope t => Value t -> OpSize
T.valueOpSize (Value (ToT a) -> OpSize) -> (a -> Value (ToT a)) -> a -> OpSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Value (ToT a)
forall a. IsoValue a => a -> Value (ToT a)
toVal
  ((SingI (ToT a), HasNoOp (ToT a)) => a -> OpSize)
-> ((KnownValue a,
     (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a))))
    :- (SingI (ToT a), HasNoOp (ToT a)))
-> a
-> OpSize
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ (KnownValue a,
 (SingI (ToT a), FailOnOperationFound (ContainsOp (ToT a))))
:- (SingI (ToT a), HasNoOp (ToT a))
forall a. NicePrintedValue a :- PrintedValScope (ToT a)
nicePrintedValueEvi @a