+type CallPat = ([Var], [CoreExpr]) -- Quantified variables and arguments
+
+
+callsToPats :: ScEnv -> [CallPat] -> [ArgOcc] -> [Call] -> UniqSM [CallPat]
+ -- Result has no duplicate patterns,
+ -- nor ones mentioned in done_pats
+callsToPats env done_pats bndr_occs calls
+ = do { mb_pats <- mapM (callToPats env bndr_occs) calls
+
+ ; let good_pats :: [([Var], [CoreArg])]
+ good_pats = catMaybes mb_pats
+ is_done p = any (samePat p) done_pats
+
+ ; return (filterOut is_done (nubBy samePat good_pats)) }
+
+callToPats :: ScEnv -> [ArgOcc] -> Call -> UniqSM (Maybe CallPat)
+ -- The [Var] is the variables to quantify over in the rule
+ -- Type variables come first, since they may scope
+ -- over the following term variables
+ -- The [CoreExpr] are the argument patterns for the rule
+callToPats env bndr_occs (con_env, args)
+ | length args < length bndr_occs -- Check saturated
+ = return Nothing
+ | otherwise
+ = do { let in_scope = substInScope (sc_subst env)
+ ; prs <- argsToPats in_scope con_env (args `zip` bndr_occs)
+ ; let (good_pats, pats) = unzip prs
+ pat_fvs = varSetElems (exprsFreeVars pats)
+ qvars = filterOut (`elemInScopeSet` in_scope) pat_fvs
+ -- Quantify over variables that are not in sccpe
+ -- at the call site
+ -- See Note [Shadowing] at the top
+
+ (tvs, ids) = partition isTyVar qvars
+ qvars' = tvs ++ ids
+ -- Put the type variables first; the type of a term
+ -- variable may mention a type variable
+
+ ; -- pprTrace "callToPats" (ppr args $$ ppr prs $$ ppr bndr_occs) $
+ if or good_pats
+ then return (Just (qvars', pats))
+ else return Nothing }
+