Skip to content

Instantly share code, notes, and snippets.

@alistra
Created December 27, 2011 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alistra/1523680 to your computer and use it in GitHub Desktop.
Save alistra/1523680 to your computer and use it in GitHub Desktop.
-- | Merges the simple 'DSInfo's based on function calls from the functions
analyzeFunctions :: [DSFun t] -> [DSInfo]
analyzeFunctions dsfs = let startingDSF = lookupDSF dsfs startingFunction in
let functions = map getDSFFun dsfs in
let startingVars = map snd $ concatMap getDSINames $ getDSFDSI startingDSF in
let runMain = mapMaybe (\var -> analyzeFunction functions startingDSF var []) startingVars in --update the accumulator
concatMap (uncurry (:)) runMain where
analyzeFunction :: [FunctionDeclaration t] -> DSFun t1 -> VariableName -> [FunctionName] -> Maybe (DSInfo, [DSInfo])
analyzeFunction functions dsf variable accumulator = let functionName = getFunName.getDSFFun $ dsf in
toMaybe (functionName `notElem` accumulator) (let functionCalls = getDSFCalls dsf in
let relevantFunctionCalls = filter (\(_, funArgs) -> Just variable `elem` funArgs) functionCalls in
let irrelevantFunctionCalls = functionCalls \\ relevantFunctionCalls in --TODO remodel so we also analyze those
let dsis = getDSFDSI dsf in
let thisVariableDSI = lookupDSI dsis variable functionName in
let otherVariablesDSIs = dsis \\ [thisVariableDSI] in
let variableBindings = map (\call@(funName, _) -> second (bindFuncall functions funName) call) relevantFunctionCalls in
let recursiveCalls = mapMaybe (\(funName, varPairs) -> (analyzeFunction functions (lookupDSF dsfs funName) (lookupJust variable varPairs) (funName:accumulator))) variableBindings in
let relevantRecursiveDSI = mconcat $ map fst recursiveCalls in
let irrelevantRecursiveDSI = concatMap snd recursiveCalls in
(thisVariableDSI `mappend` relevantRecursiveDSI, otherVariablesDSIs `union` irrelevantRecursiveDSI))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment