-----------------------------------------------------------------
-- * building the discourse history
-- ----------------------------------------------------------------------

-- See note `discourse tree'
--
-- | Take note of the fact that these discourse units have been mentioned
--   (again) in the history.
--
--   You probably want to realise the units first, then add them to the
--   history.
addToHistory :: [DiscourseUnit] -> RefHistory -> RefHistory
addToHistory ks st = st
    { -- incr number of mentions for each mention of an
      -- entity in the expression
      rhCount = foldl' plusOne    (rhCount st) (concatMap subtrees ks)
      -- note ordinal position (eg. 1st, 2nd) of any new singleton
      -- instances of each entity type we see
    , rhOrder = foldl' addOrdinal (rhOrder st) (concatMap duSingletons ks)
    }
  where
    -- | incr ref counts of 'k' in 'm'
    plusOne    m k = Map.insertWith' (+) k 1 m
    -- | note ordinal position of instances 'i' (relative to other members
    --   of class 'c'), but only if this is the first mention of it
    addOrdinal m (c,i) =
        Map.insertWith' append c [i] m
      where
        append new old = if all (`elem` old) new then old else old ++ new

-- | Individuals mentioned in a discourse unit
--   (see 'refSingleton')
duSingl
addToHistory :: [DiscourseUnit] -> RefHistory -> RefHistory
addToHistory ks st = st
    { -- incr number of mentions for each mention of an
      -- entity in the expression
      rhCount = foldl' plusOne    (rhCount st) (concatMap subtrees ks)
      -- note ordinal position (eg. 1st, 2nd) of any new singleton
      -- instances of each entity type we see
    , rhOrder = foldl' addOrdinal (rhOrder st) (concatMap duSingletons ks)
    }
  where
    -- | incr ref counts of 'k' in 'm'
    plusOne    m k = Map.insertWith' (+) k 1 m
    -- | note ordinal position of instances 'i' (relative to other members
    --   of class 'c'), but only if this is the first mention of it
    addOrdinal m (c,i) =
        Map.insertWith' append c [i] m
      where
        append new old = if all (`elem` old) new then old else old ++ new

-- | Individuals mentioned in a discourse unit
--   (see 'refSingleton')
addToHistory ks st = st
    { -- incr number of mentions for each mention of an
      -- entity in the expression
      rhCount = foldl' plusOne    (rhCount st) (concatMap subtrees ks)
      -- note ordinal position (eg. 1st, 2nd) of any new singleton
      -- instances of each entity type we see
    , rhOrder = foldl' addOrdinal (rhOrder st) (concatMap duSingletons ks)
    }
  where
    -- | incr ref counts of 'k' in 'm'
    plusOne    m k = Map.insertWith' (+) k 1 m
    -- | note ordinal position of instances 'i' (relative to other members
    --   of class 'c'), but only if this is the first mention of it
    addOrdinal m (c,i) =
        Map.insertWith' append c [i] m
      where
        append new old = if all (`elem` old) new then old else old ++ new

-- | Individuals mentioned in a discourse unit
--   (see 'refSingleton')