(|+|) :: Database -> [Clause] -> Database
db |+| sts = db `M.union` stdb
  where stdb = M.fromList $
               map (\cs -> (fst $ head cs, map snd cs)) $
               groupBy ((==) `on` fst) $
               sortBy (compare `on` fst) $
               map convert sts
        convert c = (termName c, c)
        termName (Atom s, _) = s
        termName (Compound ((Atom s) : _), _) = s
        termName _ = error "termName!"
(|+|) :: Database -> [Clause] -> Database
db |+| sts = db `M.union` stdb
  where stdb = M.fromList $
               map (\cs -> (fst $ head cs, map snd cs)) $
               groupBy ((==) `on` fst) $
               sortBy (compare `on` fst) $
               map convert sts
        convert c = (termName c, c)
        termName (Atom s, _) = s
        termName (Compound ((Atom s) : _), _) = s
        termName _ = error "termName!"
db |+| sts = db `M.union` stdb
  where stdb = M.fromList $
               map (\cs -> (fst $ head cs, map snd cs)) $
               groupBy ((==) `on` fst) $
               sortBy (compare `on` fst) $
               map convert sts
        convert c = (termName c, c)
        termName (Atom s, _) = s
        termName (Compound ((Atom s) : _), _) = s
        termName _ = error "termName!"