module PlutusIR.Analysis.Size
    ( Size (..)
    , kindSize
    , typeSize
    , tyVarDeclSize
    , termSize
    , varDeclSize
    ) where

import PlutusPrelude

import PlutusIR.Core

import PlutusCore.Size (Size (..), kindSize, tyVarDeclSize, typeSize, varDeclSize)

import Control.Lens

-- | Count the number of AST nodes in a term.
termSize :: Term tyname name uni fun ann -> Size
termSize :: forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann -> Size
termSize Term tyname name uni fun ann
term = [Size] -> Size
forall m. Monoid m => [m] -> m
forall (t :: * -> *) m. (Foldable t, Monoid m) => t m -> m
fold
    [ Integer -> Size
Size Integer
1
    , Term tyname name uni fun ann
term Term tyname name uni fun ann
-> Getting Size (Term tyname name uni fun ann) Size -> Size
forall s a. s -> Getting a s a -> a
^. (Kind ann -> Const Size (Kind ann))
-> Term tyname name uni fun ann
-> Const Size (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun ann (f :: * -> *).
Applicative f =>
(Kind ann -> f (Kind ann))
-> Term tyname name uni fun ann -> f (Term tyname name uni fun ann)
termSubkinds ((Kind ann -> Const Size (Kind ann))
 -> Term tyname name uni fun ann
 -> Const Size (Term tyname name uni fun ann))
-> ((Size -> Const Size Size) -> Kind ann -> Const Size (Kind ann))
-> Getting Size (Term tyname name uni fun ann) Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Kind ann -> Size)
-> (Size -> Const Size Size) -> Kind ann -> Const Size (Kind ann)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Kind ann -> Size
forall a. Kind a -> Size
kindSize
    , Term tyname name uni fun ann
term Term tyname name uni fun ann
-> Getting Size (Term tyname name uni fun ann) Size -> Size
forall s a. s -> Getting a s a -> a
^. (Type tyname uni ann -> Const Size (Type tyname uni ann))
-> Term tyname name uni fun ann
-> Const Size (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun a (f :: * -> *).
Applicative f =>
(Type tyname uni a -> f (Type tyname uni a))
-> Term tyname name uni fun a -> f (Term tyname name uni fun a)
termSubtypes ((Type tyname uni ann -> Const Size (Type tyname uni ann))
 -> Term tyname name uni fun ann
 -> Const Size (Term tyname name uni fun ann))
-> ((Size -> Const Size Size)
    -> Type tyname uni ann -> Const Size (Type tyname uni ann))
-> Getting Size (Term tyname name uni fun ann) Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Type tyname uni ann -> Size)
-> (Size -> Const Size Size)
-> Type tyname uni ann
-> Const Size (Type tyname uni ann)
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Type tyname uni ann -> Size
forall tyname (uni :: * -> *) ann. Type tyname uni ann -> Size
typeSize
    , Term tyname name uni fun ann
term Term tyname name uni fun ann
-> Getting Size (Term tyname name uni fun ann) Size -> Size
forall s a. s -> Getting a s a -> a
^. (Term tyname name uni fun ann
 -> Const Size (Term tyname name uni fun ann))
-> Term tyname name uni fun ann
-> Const Size (Term tyname name uni fun ann)
forall tyname name (uni :: * -> *) fun a (f :: * -> *).
Applicative f =>
(Term tyname name uni fun a -> f (Term tyname name uni fun a))
-> Term tyname name uni fun a -> f (Term tyname name uni fun a)
termSubterms ((Term tyname name uni fun ann
  -> Const Size (Term tyname name uni fun ann))
 -> Term tyname name uni fun ann
 -> Const Size (Term tyname name uni fun ann))
-> Getting Size (Term tyname name uni fun ann) Size
-> Getting Size (Term tyname name uni fun ann) Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Term tyname name uni fun ann -> Size)
-> Getting Size (Term tyname name uni fun ann) Size
forall (p :: * -> * -> *) (f :: * -> *) s a.
(Profunctor p, Contravariant f) =>
(s -> a) -> Optic' p f s a
to Term tyname name uni fun ann -> Size
forall tyname name (uni :: * -> *) fun ann.
Term tyname name uni fun ann -> Size
termSize
    ]