---------------------------------------------------------------
-- * subtle queries
-- ----------------------------------------------------------------------

-- | If it makes sense to refer to a key using an ordinal expression,
--   the order we should assign it (Nothing if we either can't sensibly
--   assign one, or the history does not give us enough information to
--   do so)
mentionOrder :: RefHistory -> RefKey -> Maybe Int
mentionOrder rh (c,i) =
   if isOnlySingletons rh
      then case Map.lookup c (rhOrder rh) of
               -- +1 is for natural language (we don't say "zeroth")
               Just is | length is > 1 -> (+ 1) <$> elemIndex i is
               _                       -> Nothing
      else Nothing
  where
     -- the c's never appear with others of their own kind in the same
     -- 'RefGroup' [we want to be able to say things like "the 5th ant"
     -- but we haven't yet worked out how this would work if 'ant' has
     -- already appeared in expressions like "the 3 ants"... in that
     -- case, what does 5th mean?]
     isOnlySingletons = not . any isMultiMatch
                      . Map.keys
                      . rhCount
     -- if a refgroup is of the given class (and has more than one elm)
     isMultiMatch (Node g _) =
         c == rgClass g && Set.size (rgIdxes g) > 1

-- | Is a subset of a previously mentioned group @g@ where there are no
-- distractors to @g@ in the discourse history
hasTidyBa
mentionOrder :: RefHistory -> RefKey -> Maybe Int
mentionOrder rh (c,i) =
   if isOnlySingletons rh
      then case Map.lookup c (rhOrder rh) of
               -- +1 is for natural language (we don't say "zeroth")
               Just is | length is > 1 -> (+ 1) <$> elemIndex i is
               _                       -> Nothing
      else Nothing
  where
     -- the c's never appear with others of their own kind in the same
     -- 'RefGroup' [we want to be able to say things like "the 5th ant"
     -- but we haven't yet worked out how this would work if 'ant' has
     -- already appeared in expressions like "the 3 ants"... in that
     -- case, what does 5th mean?]
     isOnlySingletons = not . any isMultiMatch
                      . Map.keys
                      . rhCount
     -- if a refgroup is of the given class (and has more than one elm)
     isMultiMatch (Node g _) =
         c == rgClass g && Set.size (rgIdxes g) > 1

-- | Is a subset of a previously mentioned group @g@ where there are no
-- distractors to @g@ in the discourse history
mentionOrder rh (c,i) =
   if isOnlySingletons rh
      then case Map.lookup c (rhOrder rh) of
               -- +1 is for natural language (we don't say "zeroth")
               Just is | length is > 1 -> (+ 1) <$> elemIndex i is
               _                       -> Nothing
      else Nothing
  where
     -- the c's never appear with others of their own kind in the same
     -- 'RefGroup' [we want to be able to say things like "the 5th ant"
     -- but we haven't yet worked out how this would work if 'ant' has
     -- already appeared in expressions like "the 3 ants"... in that
     -- case, what does 5th mean?]
     isOnlySingletons = not . any isMultiMatch
                      . Map.keys
                      . rhCount
     -- if a refgroup is of the given class (and has more than one elm)
     isMultiMatch (Node g _) =
         c == rgClass g && Set.size (rgIdxes g) > 1

-- | Is a subset of a previously mentioned group @g@ where there are no
-- distractors to @g@ in the discourse history