{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module PlutusIR.MkPir ( module MkPlc
                               , DatatypeDef
                               , mkLet
                               ) where

import PlutusIR

import PlutusCore.MkPlc as MkPlc

import Data.List.NonEmpty qualified as NE


-- | A datatype definition as a type variable.
type DatatypeDef tyname name uni a = Def (TyVarDecl tyname a) (Datatype tyname name uni a)

-- | Make a let binding, unless the list of bindings is empty, in which case just
-- return the underlying term.
mkLet
    :: a
    -> Recursivity
    -> [Binding tyname name uni fun a]
    -> Term tyname name uni fun a
    -> Term tyname name uni fun a
mkLet :: forall a tyname name (uni :: * -> *) fun.
a
-> Recursivity
-> [Binding tyname name uni fun a]
-> Term tyname name uni fun a
-> Term tyname name uni fun a
mkLet a
x Recursivity
r [Binding tyname name uni fun a]
bs Term tyname name uni fun a
t =  case [Binding tyname name uni fun a]
-> Maybe (NonEmpty (Binding tyname name uni fun a))
forall a. [a] -> Maybe (NonEmpty a)
NE.nonEmpty [Binding tyname name uni fun a]
bs of
  Maybe (NonEmpty (Binding tyname name uni fun a))
Nothing  -> Term tyname name uni fun a
t
  Just NonEmpty (Binding tyname name uni fun a)
bs' -> a
-> Recursivity
-> NonEmpty (Binding tyname name uni fun a)
-> Term tyname name uni fun a
-> Term tyname name uni fun a
forall tyname name (uni :: * -> *) fun a.
a
-> Recursivity
-> NonEmpty (Binding tyname name uni fun a)
-> Term tyname name uni fun a
-> Term tyname name uni fun a
Let a
x Recursivity
r NonEmpty (Binding tyname name uni fun a)
bs' Term tyname name uni fun a
t