def rules_used_in_problem(problem):
  """Returns the number of times each rule is used in this problem.

  Args:
    problem: an lib.InferenceProblem.

  Returns:
    A dictionary where keys are rule names, and values are the number of times
    the corresponding rule appears in this problem.
  """

  counts = {}
  for problem_inference in problem.inferences + problem.contradictions:
    for step in problem_inference[1]:
      rule_name = step[2]
      if rule_name not in counts:
        counts[rule_name] = 0
      counts[rule_name] += 1
  return counts