writeTheoryFiles :: HetcatsOpts -> [OutType] -> FilePath -> LibEnv
                 -> GlobalAnnos -> LibName -> IRI -> Int -> IO ()
writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n =
  let dg = lookupDGraph ln lenv
      nam = getDGNodeName $ labDG dg n
      ins = getImportNames dg n
  in case globalNodeTheory dg n of
      Nothing -> putIfVerbose opts 0 $ "could not compute theory of spec "
                 ++ show i
      Just raw_gTh0 -> do
            let tr = transNames opts
                Result es mTh = if null tr then return (raw_gTh0, "") else do
                   comor <- lookupCompComorphism (map tokStr tr) logicGraph
                   tTh <- mapG_theory comor raw_gTh0
                   return (tTh, "Translated using comorphism " ++ show comor)
                (raw_gTh, tStr) =
                  fromMaybe (raw_gTh0, "Keeping untranslated theory") mTh
            showDiags opts $ map (updDiagKind
                (\ k -> if k == Error then Warning else k)) es
            unless (null tStr) $ putIfVerbose opts 2 tStr
            putIfVerbose opts 4 $ "Sublogic of " ++ show i ++ ": " ++
                   show (sublogicOfTh raw_gTh)
            unless (modelSparQ opts == "") $
                   modelSparQCheck opts (theoremsToAxioms raw_gTh)
            mapM_ (writeTheory ins nam opts filePrefix ga raw_gTh ln i)
                 specOutTypes
writeTheoryFiles :: HetcatsOpts -> [OutType] -> FilePath -> LibEnv
                 -> GlobalAnnos -> LibName -> IRI -> Int -> IO ()
writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n =
  let dg = lookupDGraph ln lenv
      nam = getDGNodeName $ labDG dg n
      ins = getImportNames dg n
  in case globalNodeTheory dg n of
      Nothing -> putIfVerbose opts 0 $ "could not compute theory of spec "
                 ++ show i
      Just raw_gTh0 -> do
            let tr = transNames opts
                Result es mTh = if null tr then return (raw_gTh0, "") else do
                   comor <- lookupCompComorphism (map tokStr tr) logicGraph
                   tTh <- mapG_theory comor raw_gTh0
                   return (tTh, "Translated using comorphism " ++ show comor)
                (raw_gTh, tStr) =
                  fromMaybe (raw_gTh0, "Keeping untranslated theory") mTh
            showDiags opts $ map (updDiagKind
                (\ k -> if k == Error then Warning else k)) es
            unless (null tStr) $ putIfVerbose opts 2 tStr
            putIfVerbose opts 4 $ "Sublogic of " ++ show i ++ ": " ++
                   show (sublogicOfTh raw_gTh)
            unless (modelSparQ opts == "") $
                   modelSparQCheck opts (theoremsToAxioms raw_gTh)
            mapM_ (writeTheory ins nam opts filePrefix ga raw_gTh ln i)
                 specOutTypes
writeTheoryFiles opts specOutTypes filePrefix lenv ga ln i n =
  let dg = lookupDGraph ln lenv
      nam = getDGNodeName $ labDG dg n
      ins = getImportNames dg n
  in case globalNodeTheory dg n of
      Nothing -> putIfVerbose opts 0 $ "could not compute theory of spec "
                 ++ show i
      Just raw_gTh0 -> do
            let tr = transNames opts
                Result es mTh = if null tr then return (raw_gTh0, "") else do
                   comor <- lookupCompComorphism (map tokStr tr) logicGraph
                   tTh <- mapG_theory comor raw_gTh0
                   return (tTh, "Translated using comorphism " ++ show comor)
                (raw_gTh, tStr) =
                  fromMaybe (raw_gTh0, "Keeping untranslated theory") mTh
            showDiags opts $ map (updDiagKind
                (\ k -> if k == Error then Warning else k)) es
            unless (null tStr) $ putIfVerbose opts 2 tStr
            putIfVerbose opts 4 $ "Sublogic of " ++ show i ++ ": " ++
                   show (sublogicOfTh raw_gTh)
            unless (modelSparQ opts == "") $
                   modelSparQCheck opts (theoremsToAxioms raw_gTh)
            mapM_ (writeTheory ins nam opts filePrefix ga raw_gTh ln i)
                 specOutTypes