module FreeC.Backend.Coq.Converter.FuncDecl.Common
(
convertFuncHead
) where
import Control.Monad.Extra ( mapMaybeM )
import qualified FreeC.Backend.Coq.Base as Coq.Base
import FreeC.Backend.Coq.Converter.Arg
import FreeC.Backend.Coq.Converter.Free
import FreeC.Backend.Coq.Converter.Type
import qualified FreeC.Backend.Coq.Syntax as Coq
import FreeC.Environment
import qualified FreeC.IR.Syntax as IR
import FreeC.Monad.Converter
convertFuncHead
:: IR.FuncDecl -> Converter (Coq.Qualid, [Coq.Binder], Maybe Coq.Term)
convertFuncHead (IR.FuncDecl _ declIdent typeArgs args maybeRetType _) = do
let name = IR.declIdentName declIdent
Just qualid <- inEnv $ lookupIdent IR.ValueScope name
freeArgsNeeded <- inEnv $ needsFreeArgs name
let freeArgDecls | freeArgsNeeded = genericArgDecls Coq.Explicit
| otherwise = []
effects <- inEnv $ lookupEffects name
typeArgsBinder' <- convertTypeVarDecls Coq.Implicit typeArgs
typeArgsName' <- mapMaybeM
(inEnv . lookupIdent IR.TypeScope . IR.typeVarDeclQName) typeArgs
args' <- mapM convertArg args
maybeRetType' <- mapM convertType maybeRetType
return
( qualid
, freeArgDecls
++ concatMap Coq.Base.selectBinders effects
++ typeArgsBinder'
++ concatMap (flip Coq.Base.selectTypedBinders typeArgsName') effects
++ args'
, maybeRetType'
)