hydra-kernel-0.18.1: src/main/haskell/Hydra/Core/Analysis.hs
-- Note: this is an automatically generated file. Do not edit.
-- | Module dependency module name analysis
module Hydra.Core.Analysis where
import qualified Hydra.Core.Annotations as Annotations
import qualified Hydra.Core.Checking as Checking
import qualified Hydra.Core.Coders as Coders
import qualified Hydra.Core.Constants as Constants
import qualified Hydra.Core.Decode.Model as DecodeModel
import qualified Hydra.Core.Dependencies as Dependencies
import qualified Hydra.Core.Encode.Model as EncodeModel
import qualified Hydra.Core.Errors as Errors
import qualified Hydra.Core.Graph as Graph
import qualified Hydra.Core.Overlay.Haskell.Lib.Eithers as Eithers
import qualified Hydra.Core.Overlay.Haskell.Lib.Equality as Equality
import qualified Hydra.Core.Overlay.Haskell.Lib.Lists as Lists
import qualified Hydra.Core.Overlay.Haskell.Lib.Logic as Logic
import qualified Hydra.Core.Overlay.Haskell.Lib.Maps as Maps
import qualified Hydra.Core.Overlay.Haskell.Lib.Math as Math
import qualified Hydra.Core.Overlay.Haskell.Lib.Optionals as Optionals
import qualified Hydra.Core.Overlay.Haskell.Lib.Pairs as Pairs
import qualified Hydra.Core.Overlay.Haskell.Lib.Sets as Sets
import qualified Hydra.Core.Model as Model
import qualified Hydra.Core.Names as Names
import qualified Hydra.Core.Packaging as Packaging
import qualified Hydra.Core.Predicates as Predicates
import qualified Hydra.Core.Rewriting as Rewriting
import qualified Hydra.Core.Scoping as Scoping
import qualified Hydra.Core.Strip as Strip
import qualified Hydra.Core.Typing as Typing
import qualified Hydra.Core.Util as Util
import qualified Hydra.Core.Variables as Variables
import Prelude hiding (Enum, Ordering, decodeFloat, encodeFloat, fail, lines, map, pure, sum, unlines)
import qualified Data.Scientific as Sci
import Data.Void
import qualified Data.Map as M
import qualified Data.Set as S
-- | Add names to existing module names mapping
addNamesToModuleNames :: (Packaging.ModuleName -> t0) -> S.Set Model.Name -> Util.ModuleNames t0 -> Util.ModuleNames t0
addNamesToModuleNames encodeModuleName names ns0 =
let nss = Sets.fromList (Optionals.givens (Lists.map Names.moduleNameOf (Sets.toList names)))
toPair = \ns -> (ns, (encodeModuleName ns))
in Util.ModuleNames {
Util.moduleNamesFocus = (Util.moduleNamesFocus ns0),
Util.moduleNamesMapping = (Maps.union (Util.moduleNamesMapping ns0) (Maps.fromList (Lists.map toPair (Sets.toList nss))))}
-- | Find every lambda/let/forall-bound name anywhere within a list of definitions
allBinderNamesInDefinitions :: [Packaging.Definition] -> S.Set Model.Name
allBinderNamesInDefinitions defs =
let binderSetOf =
\def -> case def of
Packaging.DefinitionType v0 -> Sets.union (Sets.fromList (Model.typeSchemeVariables (Packaging.typeDefinitionBody v0))) (allBinderNamesInType (Model.typeSchemeBody (Packaging.typeDefinitionBody v0)))
Packaging.DefinitionTerm v0 -> allBinderNamesInTerm (Packaging.termDefinitionBody v0)
Packaging.DefinitionPrimitive v0 -> Sets.fromList (Model.typeSchemeVariables (Scoping.termSignatureToTypeScheme (Packaging.primitiveDefinitionSignature v0)))
in (Sets.unions (Lists.map binderSetOf defs))
-- | Find every name bound by a lambda or let-binding anywhere within a term, including nested binders
allBinderNamesInTerm :: Model.Term -> S.Set Model.Name
allBinderNamesInTerm term =
let ownBinders =
case term of
Model.TermLambda v0 -> Sets.singleton (Model.lambdaParameter v0)
Model.TermLet v0 -> Sets.fromList (Lists.map Model.bindingName (Model.letBindings v0))
_ -> Sets.empty
in (Lists.foldl (\s -> \t -> Sets.union s (allBinderNamesInTerm t)) ownBinders (Rewriting.subterms term))
-- | Find every name bound by a forall anywhere within a type, including nested binders
allBinderNamesInType :: Model.Type -> S.Set Model.Name
allBinderNamesInType typ =
let ownBinders =
case typ of
Model.TypeForall v0 -> Sets.singleton (Model.forallTypeParameter v0)
_ -> Sets.empty
in (Lists.foldl (\s -> \t -> Sets.union s (allBinderNamesInType t)) ownBinders (Rewriting.subtypes typ))
-- | Analyze a function term, collecting lambdas, type lambdas, lets, and type applications
analyzeFunctionTerm :: Typing.InferenceContext -> (t0 -> Graph.Graph) -> (Graph.Graph -> t0 -> t0) -> t0 -> Model.Term -> Either t1 (Typing.FunctionStructure t0)
analyzeFunctionTerm cx getTC setTC env term =
analyzeFunctionTermWith cx (\g -> \b -> Logic.ifElse (Predicates.isComplexBinding g b) (Just (Model.TermLiteral (Model.LiteralBoolean True))) Nothing) getTC setTC env term
-- | Analyze a function term with configurable binding metadata
analyzeFunctionTermWith :: Typing.InferenceContext -> (Graph.Graph -> Model.Binding -> Maybe Model.Term) -> (t0 -> Graph.Graph) -> (Graph.Graph -> t0 -> t0) -> t0 -> Model.Term -> Either t1 (Typing.FunctionStructure t0)
analyzeFunctionTermWith cx forBinding getTC setTC env term =
analyzeFunctionTermWithGather cx forBinding getTC setTC True env [] [] [] [] [] term
-- | Final step of the function-term walk: type-apply the body and assemble the FunctionStructure
analyzeFunctionTermWithFinish :: Typing.InferenceContext -> (t0 -> Graph.Graph) -> t0 -> [Model.Name] -> [Model.Name] -> [Model.Binding] -> [Model.Type] -> [Model.Type] -> Model.Term -> Either t1 (Typing.FunctionStructure t0)
analyzeFunctionTermWithFinish cx getTC fEnv tparams args bindings doms tapps body =
let bodyWithTapps =
Lists.foldl (\trm -> \typ -> Model.TermTypeApplication (Model.TypeApplicationTerm {
Model.typeApplicationTermBody = trm,
Model.typeApplicationTermType = typ})) body tapps
mcod = Eithers.either (\_ -> Nothing) (\c -> Just c) (Checking.typeOfTerm cx (getTC fEnv) bodyWithTapps)
in (Right (Typing.FunctionStructure {
Typing.functionStructureTypeParams = (Lists.reverse tparams),
Typing.functionStructureParams = (Lists.reverse args),
Typing.functionStructureBindings = bindings,
Typing.functionStructureBody = bodyWithTapps,
Typing.functionStructureDomains = (Lists.reverse doms),
Typing.functionStructureCodomain = mcod,
Typing.functionStructureEnvironment = fEnv}))
-- | Recursive step of the function-term walk: peel lambdas / type-lambdas / type-applications, accumulating params and bindings, then call analyzeFunctionTermWithFinish
analyzeFunctionTermWithGather :: Typing.InferenceContext -> (Graph.Graph -> Model.Binding -> Maybe Model.Term) -> (t0 -> Graph.Graph) -> (Graph.Graph -> t0 -> t0) -> Bool -> t0 -> [Model.Name] -> [Model.Name] -> [Model.Binding] -> [Model.Type] -> [Model.Type] -> Model.Term -> Either t1 (Typing.FunctionStructure t0)
analyzeFunctionTermWithGather cx forBinding getTC setTC argMode gEnv tparams args bindings doms tapps t =
case (Strip.deannotateTerm t) of
Model.TermLambda v0 -> Logic.ifElse argMode (
let v = Model.lambdaParameter v0
dom = Optionals.match (Model.lambdaDomain v0) (Model.TypeVariable (Model.Name "_")) (\x_ -> x_)
body = Model.lambdaBody v0
newEnv = setTC (Scoping.extendGraphForLambda (getTC gEnv) v0) gEnv
in (analyzeFunctionTermWithGather cx forBinding getTC setTC argMode newEnv tparams (Lists.cons v args) bindings (Lists.cons dom doms) tapps body)) (analyzeFunctionTermWithFinish cx getTC gEnv tparams args bindings doms tapps t)
Model.TermLet v0 ->
let inScope0 = Sets.union (Sets.fromList args) (Sets.fromList (Lists.map Model.bindingName bindings))
resolveBinding =
\acc -> \b ->
let bname = Model.bindingName b
resolvedSoFar = Pairs.first acc
scopeSoFar = Pairs.second acc
in (Logic.ifElse (Sets.member bname scopeSoFar) (
let bname2 = Variables.freshNameAvoiding bname scopeSoFar
in (Lists.cons (bname, bname2) resolvedSoFar, (Sets.insert bname2 scopeSoFar))) (resolvedSoFar, (Sets.insert bname scopeSoFar)))
resolveState = Lists.foldl resolveBinding ([], inScope0) (Model.letBindings v0)
renames = Maps.fromList (Pairs.first resolveState)
renameBindingName =
\b -> Model.Binding {
Model.bindingName = (Optionals.withDefault (Model.bindingName b) (Maps.lookup (Model.bindingName b) renames)),
Model.bindingTerm = (Variables.substituteVariables renames (Model.bindingTerm b)),
Model.bindingTypeScheme = (Model.bindingTypeScheme b)}
newBindings = Lists.map renameBindingName (Model.letBindings v0)
body = Logic.ifElse (Maps.isEmpty renames) (Model.letBody v0) (Variables.substituteVariables renames (Model.letBody v0))
newEnv = setTC (Scoping.extendGraphForLet forBinding (getTC gEnv) v0) gEnv
in (analyzeFunctionTermWithGather cx forBinding getTC setTC False newEnv tparams args (Lists.concat2 bindings newBindings) doms tapps body)
Model.TermTypeApplication v0 ->
let taBody = Model.typeApplicationTermBody v0
typ = Model.typeApplicationTermType v0
in (analyzeFunctionTermWithGather cx forBinding getTC setTC argMode gEnv tparams args bindings doms (Lists.cons typ tapps) taBody)
Model.TermTypeLambda v0 ->
let tvar = Model.typeLambdaParameter v0
tlBody = Model.typeLambdaBody v0
newEnv = setTC (Scoping.extendGraphForTypeLambda (getTC gEnv) v0) gEnv
in (analyzeFunctionTermWithGather cx forBinding getTC setTC argMode newEnv (Lists.cons tvar tparams) args bindings doms tapps tlBody)
_ -> analyzeFunctionTermWithFinish cx getTC gEnv tparams args bindings doms tapps t
-- | Get dependency module names from definitions
definitionDependencyModuleNames :: [Packaging.Definition] -> S.Set Packaging.ModuleName
definitionDependencyModuleNames defs =
let allNames = Sets.unions (Lists.map definitionReferencedNames defs)
in (Sets.fromList (Optionals.givens (Lists.map Names.moduleNameOf (Sets.toList allNames))))
-- | Get, per dependency module, the counted multiset of its local names referenced by the definitions
definitionDependencyNamesByModule :: [Packaging.Definition] -> M.Map Packaging.ModuleName (M.Map String Int)
definitionDependencyNamesByModule defs =
let pairs =
Lists.concat (Lists.map (\def -> Optionals.givens (Lists.map (\nm -> Optionals.map (\mn -> (mn, (Names.localNameOf nm))) (Names.moduleNameOf nm)) (Sets.toList (definitionReferencedNames def)))) defs)
in (Lists.foldl (\acc -> \p ->
let mn = Pairs.first p
local = Pairs.second p
inner = Optionals.withDefault Maps.empty (Maps.lookup mn acc)
newCount = Math.add 1 (Optionals.withDefault 0 (Maps.lookup local inner))
in (Maps.insert mn (Maps.insert local newCount inner) acc)) Maps.empty pairs)
-- | Get the names referenced by a definition, including the primitive-shape names for a primitive
definitionReferencedNames :: Packaging.Definition -> S.Set Model.Name
definitionReferencedNames def =
case def of
Packaging.DefinitionType v0 -> Dependencies.typeDependencyNames True (Model.typeSchemeBody (Packaging.typeDefinitionBody v0))
Packaging.DefinitionTerm v0 -> Dependencies.termDependencyNames True True True (Packaging.termDefinitionBody v0)
Packaging.DefinitionPrimitive v0 -> Sets.union (Sets.fromList [
Model.Name "hydra.core.model.Type",
(Model.Name "hydra.core.packaging.PrimitiveDefinition"),
(Model.Name "hydra.core.typing.TermSignature")]) (Sets.union (Dependencies.typeDependencyNames True (Model.typeSchemeBody (Scoping.termSignatureToTypeScheme (Packaging.primitiveDefinitionSignature v0)))) (Optionals.match (Packaging.primitiveDefinitionDefaultImplementation v0) Sets.empty (\impl -> Dependencies.termDependencyNames True True True impl)))
-- | Find dependency module names in all of a set of terms (Either version)
dependencyModuleNames :: t0 -> Graph.Graph -> Bool -> Bool -> Bool -> Bool -> [Model.Binding] -> Either Errors.Error (S.Set Packaging.ModuleName)
dependencyModuleNames cx graph binds withPrims withNoms withSchema els =
let depNames =
\el ->
let term = Model.bindingTerm el
deannotatedTerm = Strip.deannotateTerm term
dataNames = Dependencies.termDependencyNames binds withPrims withNoms term
schemaNames =
Logic.ifElse withSchema (Optionals.match (Model.bindingTypeScheme el) Sets.empty (\ts -> Dependencies.typeDependencyNames True (Model.typeSchemeBody ts))) Sets.empty
in (Logic.ifElse (Predicates.isEncodedType deannotatedTerm) (Eithers.map (\typ -> Sets.unions [
dataNames,
schemaNames,
(Dependencies.typeDependencyNames True typ)]) (Eithers.bimap (\_e -> Errors.ErrorDecoding _e) (\_a -> _a) (DecodeModel.type_ graph term))) (Logic.ifElse (Predicates.isEncodedTerm deannotatedTerm) (Eithers.map (\decodedTerm -> Sets.unions [
dataNames,
schemaNames,
(Dependencies.termDependencyNames binds withPrims withNoms decodedTerm)]) (Eithers.bimap (\_e -> Errors.ErrorDecoding _e) (\_a -> _a) (DecodeModel.term graph term))) (Right (Sets.unions [
dataNames,
schemaNames]))))
in (Eithers.map (\namesList -> Sets.fromList (Optionals.givens (Lists.map Names.moduleNameOf (Sets.toList (Sets.unions namesList))))) (Eithers.mapList depNames els))
-- | Gather applications from a term, returning (args, baseTerm)
gatherApplications :: Model.Term -> ([Model.Term], Model.Term)
gatherApplications term =
let go =
\args -> \t -> case (Strip.deannotateTerm t) of
Model.TermApplication v0 ->
let lhs = Model.applicationFunction v0
rhs = Model.applicationArgument v0
in (go (Lists.cons rhs args) lhs)
_ -> (args, t)
in (go [] term)
-- | Gather term arguments, stripping type-level constructs
gatherArgs :: Model.Term -> [Model.Term] -> (Model.Term, [Model.Term])
gatherArgs term args =
case (Strip.deannotateTerm term) of
Model.TermApplication v0 ->
let lhs = Model.applicationFunction v0
rhs = Model.applicationArgument v0
in (gatherArgs lhs (Lists.cons rhs args))
Model.TermTypeLambda v0 ->
let body = Model.typeLambdaBody v0
in (gatherArgs body args)
Model.TermTypeApplication v0 ->
let body = Model.typeApplicationTermBody v0
in (gatherArgs body args)
_ -> (term, args)
-- | Gather term and type arguments from a term
gatherArgsWithTypeApps :: Model.Term -> [Model.Term] -> [Model.Type] -> (Model.Term, ([Model.Term], [Model.Type]))
gatherArgsWithTypeApps term args tyArgs =
case (Strip.deannotateTerm term) of
Model.TermApplication v0 ->
let lhs = Model.applicationFunction v0
rhs = Model.applicationArgument v0
in (gatherArgsWithTypeApps lhs (Lists.cons rhs args) tyArgs)
Model.TermTypeLambda v0 ->
let body = Model.typeLambdaBody v0
in (gatherArgsWithTypeApps body args tyArgs)
Model.TermTypeApplication v0 ->
let body = Model.typeApplicationTermBody v0
typ = Model.typeApplicationTermType v0
in (gatherArgsWithTypeApps body args (Lists.cons typ tyArgs))
_ -> (term, (args, tyArgs))
-- | Check if a term body is self-tail-recursive with respect to a function name
isSelfTailRecursive :: Model.Name -> Model.Term -> Bool
isSelfTailRecursive funcName body =
let callsSelf = Logic.not (Variables.isFreeVariableInTerm funcName body)
in (Logic.ifElse callsSelf (isTailRecursiveInTailPosition funcName body) False)
-- | Check if a term can be encoded as a simple assignment
isSimpleAssignment :: Model.Term -> Bool
isSimpleAssignment term =
case term of
Model.TermAnnotated v0 -> isSimpleAssignment (Model.annotatedTermBody v0)
Model.TermLambda _ -> False
Model.TermLet _ -> False
Model.TermTypeLambda _ -> False
Model.TermTypeApplication v0 -> isSimpleAssignment (Model.typeApplicationTermBody v0)
_ ->
let baseTerm = Pairs.first (gatherArgs term [])
in case baseTerm of
Model.TermCases _ -> False
_ -> True
-- | Check that all self-references are in tail position
isTailRecursiveInTailPosition :: Model.Name -> Model.Term -> Bool
isTailRecursiveInTailPosition funcName term =
let stripped = Strip.deannotateAndDetypeTerm term
in case stripped of
Model.TermApplication _ ->
let gathered = gatherApplications stripped
gatherArgs = Pairs.first gathered
gatherFun = Pairs.second gathered
strippedFun = Strip.deannotateAndDetypeTerm gatherFun
in case strippedFun of
Model.TermVariable v1 -> Logic.ifElse (Equality.equal v1 funcName) (
let argsNoFunc = Lists.foldl (\ok -> \arg -> Logic.and ok (Variables.isFreeVariableInTerm funcName arg)) True gatherArgs
argsNoLambda =
Lists.foldl (\ok -> \arg -> Logic.and ok (Logic.not (Rewriting.foldOverTerm Coders.TraversalOrderPre (\found -> \t -> Logic.or found (case t of
Model.TermLambda v2 ->
let ignore = Model.lambdaBody v2
in True
_ -> False)) False arg))) True gatherArgs
in (Logic.and argsNoFunc argsNoLambda)) (Variables.isFreeVariableInTerm funcName term)
Model.TermCases v1 ->
let cases_ = Model.caseStatementCases v1
dflt = Model.caseStatementDefault v1
branchesOk =
Lists.foldl (\ok -> \field -> Logic.and ok (isTailRecursiveInTailPosition funcName (Model.caseAlternativeHandler field))) True cases_
dfltOk = Optionals.match dflt True (\d -> isTailRecursiveInTailPosition funcName d)
argsOk = Lists.foldl (\ok -> \arg -> Logic.and ok (Variables.isFreeVariableInTerm funcName arg)) True gatherArgs
in (Logic.and (Logic.and branchesOk dfltOk) argsOk)
_ -> Variables.isFreeVariableInTerm funcName term
Model.TermLambda v0 -> isTailRecursiveInTailPosition funcName (Model.lambdaBody v0)
Model.TermLet v0 ->
let bindingsOk =
Lists.foldl (\ok -> \b -> Logic.and ok (Variables.isFreeVariableInTerm funcName (Model.bindingTerm b))) True (Model.letBindings v0)
in (Logic.and bindingsOk (isTailRecursiveInTailPosition funcName (Model.letBody v0)))
_ -> Variables.isFreeVariableInTerm funcName term
-- | Lower a self-tail-recursive function body into an explicit TcoLoopPlan, or Nothing if the body is not self-tail-recursive. Mirrors the traversal of isTailRecursiveInTailPosition, but builds a TcoStep tree instead of a boolean verdict.
lowerSelfTailRecursion :: Model.Name -> [Model.Name] -> Model.Term -> Maybe Coders.TcoLoopPlan
lowerSelfTailRecursion funcName paramNames body =
Logic.ifElse (isSelfTailRecursive funcName body) (Just (Coders.TcoLoopPlan {
Coders.tcoLoopPlanParams = paramNames,
Coders.tcoLoopPlanBody = (lowerTcoStep funcName paramNames body)})) Nothing
-- | Lower a term in tail position into a TcoStep (continue/return/cases/let_)
lowerTcoStep :: Model.Name -> t0 -> Model.Term -> Coders.TcoStep
lowerTcoStep funcName paramNames term =
let stripped = Strip.deannotateAndDetypeTerm term
in case stripped of
Model.TermApplication _ ->
let gathered = gatherApplications stripped
gatherArgs = Pairs.first gathered
gatherFun = Pairs.second gathered
strippedFun = Strip.deannotateAndDetypeTerm gatherFun
in case strippedFun of
Model.TermVariable v1 -> Logic.ifElse (Equality.equal v1 funcName) (Coders.TcoStepContinue gatherArgs) (Coders.TcoStepReturn term)
Model.TermCases v1 ->
let tname = Model.caseStatementTypeName v1
cases_ = Model.caseStatementCases v1
dflt = Model.caseStatementDefault v1
loweredCases =
Lists.map (\field ->
let handler = Model.caseAlternativeHandler field
strippedHandler = Strip.deannotateAndDetypeTerm handler
in case strippedHandler of
Model.TermLambda v2 ->
let param = Model.lambdaParameter v2
lamBody = Model.lambdaBody v2
in Coders.TcoCaseAlternative {
Coders.tcoCaseAlternativeName = (Model.caseAlternativeName field),
Coders.tcoCaseAlternativeParameter = param,
Coders.tcoCaseAlternativeHandler = (lowerTcoStep funcName paramNames lamBody)}
_ -> Coders.TcoCaseAlternative {
Coders.tcoCaseAlternativeName = (Model.caseAlternativeName field),
Coders.tcoCaseAlternativeParameter = (Model.Name "_"),
Coders.tcoCaseAlternativeHandler = (lowerTcoStep funcName paramNames handler)}) cases_
loweredDflt = Optionals.map (\d -> lowerTcoStep funcName paramNames d) dflt
scrutinee_ = Optionals.withDefault term (Lists.head gatherArgs)
in (Coders.TcoStepCases (Coders.TcoCaseStatement {
Coders.tcoCaseStatementTypeName = tname,
Coders.tcoCaseStatementScrutinee = scrutinee_,
Coders.tcoCaseStatementDefault = loweredDflt,
Coders.tcoCaseStatementCases = loweredCases}))
_ -> Coders.TcoStepReturn term
Model.TermLambda v0 -> lowerTcoStep funcName paramNames (Model.lambdaBody v0)
Model.TermLet v0 ->
let loweredBindings =
Lists.map (\b -> Coders.TcoBinding {
Coders.tcoBindingName = (Model.bindingName b),
Coders.tcoBindingTerm = (Model.bindingTerm b),
Coders.tcoBindingTypeScheme = (Model.bindingTypeScheme b)}) (Model.letBindings v0)
in (Coders.TcoStepLet_ (Coders.TcoLet {
Coders.tcoLetBindings = loweredBindings,
Coders.tcoLetBody = (lowerTcoStep funcName paramNames (Model.letBody v0))}))
_ -> Coders.TcoStepReturn term
-- | Check whether a module contains any binary literal values
moduleContainsBinaryLiterals :: Packaging.Module -> Bool
moduleContainsBinaryLiterals mod =
let checkTerm =
\found -> \term -> Logic.or found (case term of
Model.TermLiteral v0 -> case v0 of
Model.LiteralBinary _ -> True
_ -> False
_ -> False)
termContainsBinary = \term -> Rewriting.foldOverTerm Coders.TraversalOrderPre checkTerm False term
defTerms =
Optionals.givens (Lists.map (\d -> case d of
Packaging.DefinitionTerm v0 -> Just (Packaging.termDefinitionBody v0)
_ -> Nothing) (Packaging.moduleDefinitions mod))
in (Lists.foldl (\acc -> \t -> Logic.or acc (termContainsBinary t)) False defTerms)
-- | Check whether a module contains any decimal literal values
moduleContainsDecimalLiterals :: Packaging.Module -> Bool
moduleContainsDecimalLiterals mod =
let checkTerm =
\found -> \term -> Logic.or found (case term of
Model.TermLiteral v0 -> case v0 of
Model.LiteralDecimal _ -> True
_ -> False
_ -> False)
termContainsDecimal = \term -> Rewriting.foldOverTerm Coders.TraversalOrderPre checkTerm False term
defTerms =
Optionals.givens (Lists.map (\d -> case d of
Packaging.DefinitionTerm v0 -> Just (Packaging.termDefinitionBody v0)
_ -> Nothing) (Packaging.moduleDefinitions mod))
in (Lists.foldl (\acc -> \t -> Logic.or acc (termContainsDecimal t)) False defTerms)
-- | Find dependency module names in all elements of a module, excluding the module's own module name (Either version)
moduleDependencyModuleNames :: t0 -> Graph.Graph -> Bool -> Bool -> Bool -> Bool -> Packaging.Module -> Either Errors.Error (S.Set Packaging.ModuleName)
moduleDependencyModuleNames cx graph binds withPrims withNoms withSchema mod =
let allBindings =
Optionals.givens (Lists.map (\d -> case d of
Packaging.DefinitionType v0 -> Just ((\name -> \typ ->
let schemaTerm = Model.TermVariable (Model.Name "hydra.core.model.Type")
dataTerm =
Annotations.normalizeTermAnnotations (Model.TermAnnotated (Model.AnnotatedTerm {
Model.annotatedTermBody = (EncodeModel.type_ typ),
Model.annotatedTermAnnotation = (Annotations.wrapAnnotationMap (Maps.fromList [
(Constants.keyType, schemaTerm)]))}))
in Model.Binding {
Model.bindingName = name,
Model.bindingTerm = dataTerm,
Model.bindingTypeScheme = (Just (Model.TypeScheme {
Model.typeSchemeVariables = [],
Model.typeSchemeBody = (Model.TypeVariable (Model.Name "hydra.core.model.Type")),
Model.typeSchemeConstraints = Maps.empty}))}) (Packaging.typeDefinitionName v0) (Model.typeSchemeBody (Packaging.typeDefinitionBody v0)))
Packaging.DefinitionTerm v0 -> Just (Model.Binding {
Model.bindingName = (Packaging.termDefinitionName v0),
Model.bindingTerm = (Packaging.termDefinitionBody v0),
Model.bindingTypeScheme = (Optionals.map Scoping.termSignatureToTypeScheme (Packaging.termDefinitionSignature v0))})
_ -> Nothing) (Packaging.moduleDefinitions mod))
in (Eithers.map (\deps -> Sets.delete (Packaging.moduleName mod) deps) (dependencyModuleNames cx graph binds withPrims withNoms withSchema allBindings))
-- | Create module names mapping for definitions
moduleNamesForDefinitions :: (Packaging.ModuleName -> t0) -> Packaging.ModuleName -> [Packaging.Definition] -> Util.ModuleNames t0
moduleNamesForDefinitions encodeModuleName focusNs defs =
let nss = Sets.delete focusNs (definitionDependencyModuleNames defs)
toPair = \ns -> (ns, (encodeModuleName ns))
in Util.ModuleNames {
Util.moduleNamesFocus = (toPair focusNs),
Util.moduleNamesMapping = (Maps.fromList (Lists.map toPair (Sets.toList nss)))}