-- oneElem :: (Ord a, Show a, Num a) => [a] -> a
-- oneElem xs = case S.toList (S.fromList xs) of
--   []  -> 0
--   [x] -> x
--   _ -> error $ "oneElem: |" ++ show xs ++ "| <> 1"



------------------------------------------------------
-- Merging
------------------------------------------------------


-- | Merge a list of DAGs.
merge :: (Ord t) => [DAG (Token t)] -> DAG (Token t)
merge dags =
  [ Edge
    { from  = from
    , to    = to
    , label = label }
  | ((from, to), label) <- M.toList dagMap ]
  where
    dagMap = M.fromListWith mergeTok
      [ ((from, to), label)
      | Edge{..} <- concat dags ]
    mergeTok x y = mergeToks [x, y]


-- | Assumption: input list non-empty and all tokens have
-- the same `orth` value.
merge :: (Ord t) => [DAG (Token t)] -> DAG (Token t)
merge dags =
  [ Edge
    { from  = from
    , to    = to
    , label = label }
  | ((from, to), label) <- M.toList dagMap ]
  where
    dagMap = M.fromListWith mergeTok
      [ ((from, to), label)
      | Edge{..} <- concat dags ]
    mergeTok x y = mergeToks [x, y]


-- | Assumption: input list non-empty and all tokens have
-- the same `orth` value.
merge dags =
  [ Edge
    { from  = from
    , to    = to
    , label = label }
  | ((from, to), label) <- M.toList dagMap ]
  where
    dagMap = M.fromListWith mergeTok
      [ ((from, to), label)
      | Edge{..} <- concat dags ]
    mergeTok x y = mergeToks [x, y]


-- | Assumption: input list non-empty and all tokens have
-- the same `orth` value.