writeSpecFiles :: HetcatsOpts -> FilePath -> LibEnv -> LibName -> DGraph
               -> IO ()
writeSpecFiles opts file lenv ln dg = do
    let gctx = globalEnv dg
        gns = Map.keys gctx
        mns = map $ \ t -> Map.findWithDefault (simpleIdToIRI t) (tokStr t)
          $ Map.fromList $ map (\ i -> (iriToStringShortUnsecure i, i)) gns
        ga = globalAnnos dg
        ns = mns $ specNames opts
        vs = mns $ viewNames opts
        filePrefix = snd $ getFilePrefix opts file
        outTypes = outtypes opts
        specOutTypes = filter ( \ ot -> case ot of
            ThyFile -> True
            DfgFile _ -> True
            TPTPFile _ -> True
            XmlOut -> True
            JsonOut -> True
            OmdocOut -> True
            TheoryFile _ -> True
            SigFile _ -> True
            OWLOut _ -> True
            CLIFOut -> True
            KIFOut -> True
            FreeCADOut -> True
            HaskellOut -> True
            ComptableXml -> True
            SymXml -> True
            _ -> False) outTypes
        allSpecs = null ns
        noViews = null vs
        ignore = null specOutTypes && modelSparQ opts == ""
    mapM_ (writeLibEnv opts filePrefix lenv ln) $
          if null $ dumpOpts opts then outTypes else EnvOut : outTypes
    mapM_ ( \ i -> case Map.lookup i gctx of
        Just (SpecEntry (ExtGenSig _ (NodeSig n _))) ->
            writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n
        _ -> unless allSpecs
               $ putIfVerbose opts 0 $ "Unknown spec name: " ++ show i
      ) $ if ignore then [] else
        if allSpecs then gns else ns
    unless noViews $
      mapM_ ( \ i -> case Map.lookup i gctx of
        Just (ViewOrStructEntry _ (ExtViewSig _ (GMorphism cid _ _ m _) _)) ->
            writeVerbFile opts (filePrefix ++ "_" ++ show i ++ ".view")
              $ shows (pretty $ Map.toList $ symmap_of (targetLogic cid) m) "\n"
        _ -> putIfVerbose opts 0 $ "Unknown view name: " ++ show i
      ) vs
    mapM_ ( \ n ->
      writeTheoryFiles opts specOutTypes filePrefix lenv ga ln
         (simpleIdToIRI $ genToken $ 'n' : show n) n)
      $ if ignore || not allSpecs then [] else
      nodesDG dg
      \\ Map.fold ( \ e l -> case e of
            SpecEntry (ExtGenSig _ (NodeSig n _)) -> n : l
            _ -> l) [] gctx
    doDump opts "GlobalAnnos" $ putStrLn $ showGlobalDoc ga ga ""
    doDump opts "PrintStat" $ putStrLn $ printStatistics dg
    doDump opts "DGraph" $ putStrLn $ showDoc dg ""
    doDump opts "DuplicateDefEdges" $ let es = duplicateDefEdges dg in
      unless (null es) $ print es
    doDump opts "LibEnv" $ writeVerbFile opts (filePrefix ++ ".lenv")
      $ shows (DG.prettyLibEnv lenv) "\n"
writeSpecFiles :: HetcatsOpts -> FilePath -> LibEnv -> LibName -> DGraph
               -> IO ()
writeSpecFiles opts file lenv ln dg = do
    let gctx = globalEnv dg
        gns = Map.keys gctx
        mns = map $ \ t -> Map.findWithDefault (simpleIdToIRI t) (tokStr t)
          $ Map.fromList $ map (\ i -> (iriToStringShortUnsecure i, i)) gns
        ga = globalAnnos dg
        ns = mns $ specNames opts
        vs = mns $ viewNames opts
        filePrefix = snd $ getFilePrefix opts file
        outTypes = outtypes opts
        specOutTypes = filter ( \ ot -> case ot of
            ThyFile -> True
            DfgFile _ -> True
            TPTPFile _ -> True
            XmlOut -> True
            JsonOut -> True
            OmdocOut -> True
            TheoryFile _ -> True
            SigFile _ -> True
            OWLOut _ -> True
            CLIFOut -> True
            KIFOut -> True
            FreeCADOut -> True
            HaskellOut -> True
            ComptableXml -> True
            SymXml -> True
            _ -> False) outTypes
        allSpecs = null ns
        noViews = null vs
        ignore = null specOutTypes && modelSparQ opts == ""
    mapM_ (writeLibEnv opts filePrefix lenv ln) $
          if null $ dumpOpts opts then outTypes else EnvOut : outTypes
    mapM_ ( \ i -> case Map.lookup i gctx of
        Just (SpecEntry (ExtGenSig _ (NodeSig n _))) ->
            writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n
        _ -> unless allSpecs
               $ putIfVerbose opts 0 $ "Unknown spec name: " ++ show i
      ) $ if ignore then [] else
        if allSpecs then gns else ns
    unless noViews $
      mapM_ ( \ i -> case Map.lookup i gctx of
        Just (ViewOrStructEntry _ (ExtViewSig _ (GMorphism cid _ _ m _) _)) ->
            writeVerbFile opts (filePrefix ++ "_" ++ show i ++ ".view")
              $ shows (pretty $ Map.toList $ symmap_of (targetLogic cid) m) "\n"
        _ -> putIfVerbose opts 0 $ "Unknown view name: " ++ show i
      ) vs
    mapM_ ( \ n ->
      writeTheoryFiles opts specOutTypes filePrefix lenv ga ln
         (simpleIdToIRI $ genToken $ 'n' : show n) n)
      $ if ignore || not allSpecs then [] else
      nodesDG dg
      \\ Map.fold ( \ e l -> case e of
            SpecEntry (ExtGenSig _ (NodeSig n _)) -> n : l
            _ -> l) [] gctx
    doDump opts "GlobalAnnos" $ putStrLn $ showGlobalDoc ga ga ""
    doDump opts "PrintStat" $ putStrLn $ printStatistics dg
    doDump opts "DGraph" $ putStrLn $ showDoc dg ""
    doDump opts "DuplicateDefEdges" $ let es = duplicateDefEdges dg in
      unless (null es) $ print es
    doDump opts "LibEnv" $ writeVerbFile opts (filePrefix ++ ".lenv")
      $ shows (DG.prettyLibEnv lenv) "\n"
writeSpecFiles opts file lenv ln dg = do
    let gctx = globalEnv dg
        gns = Map.keys gctx
        mns = map $ \ t -> Map.findWithDefault (simpleIdToIRI t) (tokStr t)
          $ Map.fromList $ map (\ i -> (iriToStringShortUnsecure i, i)) gns
        ga = globalAnnos dg
        ns = mns $ specNames opts
        vs = mns $ viewNames opts
        filePrefix = snd $ getFilePrefix opts file
        outTypes = outtypes opts
        specOutTypes = filter ( \ ot -> case ot of
            ThyFile -> True
            DfgFile _ -> True
            TPTPFile _ -> True
            XmlOut -> True
            JsonOut -> True
            OmdocOut -> True
            TheoryFile _ -> True
            SigFile _ -> True
            OWLOut _ -> True
            CLIFOut -> True
            KIFOut -> True
            FreeCADOut -> True
            HaskellOut -> True
            ComptableXml -> True
            SymXml -> True
            _ -> False) outTypes
        allSpecs = null ns
        noViews = null vs
        ignore = null specOutTypes && modelSparQ opts == ""
    mapM_ (writeLibEnv opts filePrefix lenv ln) $
          if null $ dumpOpts opts then outTypes else EnvOut : outTypes
    mapM_ ( \ i -> case Map.lookup i gctx of
        Just (SpecEntry (ExtGenSig _ (NodeSig n _))) ->
            writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n
        _ -> unless allSpecs
               $ putIfVerbose opts 0 $ "Unknown spec name: " ++ show i
      ) $ if ignore then [] else
        if allSpecs then gns else ns
    unless noViews $
      mapM_ ( \ i -> case Map.lookup i gctx of
        Just (ViewOrStructEntry _ (ExtViewSig _ (GMorphism cid _ _ m _) _)) ->
            writeVerbFile opts (filePrefix ++ "_" ++ show i ++ ".view")
              $ shows (pretty $ Map.toList $ symmap_of (targetLogic cid) m) "\n"
        _ -> putIfVerbose opts 0 $ "Unknown view name: " ++ show i
      ) vs
    mapM_ ( \ n ->
      writeTheoryFiles opts specOutTypes filePrefix lenv ga ln
         (simpleIdToIRI $ genToken $ 'n' : show n) n)
      $ if ignore || not allSpecs then [] else
      nodesDG dg
      \\ Map.fold ( \ e l -> case e of
            SpecEntry (ExtGenSig _ (NodeSig n _)) -> n : l
            _ -> l) [] gctx
    doDump opts "GlobalAnnos" $ putStrLn $ showGlobalDoc ga ga ""
    doDump opts "PrintStat" $ putStrLn $ printStatistics dg
    doDump opts "DGraph" $ putStrLn $ showDoc dg ""
    doDump opts "DuplicateDefEdges" $ let es = duplicateDefEdges dg in
      unless (null es) $ print es
    doDump opts "LibEnv" $ writeVerbFile opts (filePrefix ++ ".lenv")
      $ shows (DG.prettyLibEnv lenv) "\n"