writeTheory :: [String] -> String -> HetcatsOpts -> FilePath -> GlobalAnnos
  -> G_theory -> LibName -> IRI -> OutType -> IO ()
writeTheory ins nam opts filePrefix ga
  raw_gTh@(G_theory lid _ (ExtSign sign0 _) _ sens0 _) ln i ot =
    let fp = filePrefix ++ "_" ++ i'
        i' = encode (iriToStringShortUnsecure $ setAngles False i)
        f = fp ++ "." ++ show ot
        th = (sign0, toNamedList sens0)
        lang = language_name lid
    in case ot of
    FreeCADOut -> writeFreeCADFile opts filePrefix raw_gTh
    ThyFile -> writeIsaFile opts filePrefix raw_gTh ln i
    DfgFile c -> writeSoftFOL opts f raw_gTh i c 0 "DFG"
    TPTPFile c -> writeSoftFOL opts f raw_gTh i c 1 "TPTP"
    TheoryFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (DG.printTh ga i raw_gTh) "\n"
        else putIfVerbose opts 0 "printing theory delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', sens') = addUniformRestr sign sens
            lse = map (namedSenToSExpr sign') sens'
        unless (null lse) $ writeVerbFile opts (fp ++ ".sexpr")
            $ shows (prettySExpr $ SList lse) "\n"
    SigFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (pretty $ signOf raw_gTh) "\n"
        else putIfVerbose opts 0 "printing signature delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', _sens') = addUniformRestr sign sens
        writeVerbFile opts (f ++ ".sexpr")
          $ shows (prettySExpr $ vseSignToSExpr sign') "\n"
    SymXml -> writeVerbFile opts f $ ppTopElement
           $ ToXml.showSymbolsTh ins nam ga raw_gTh
#ifdef PROGRAMATICA
    HaskellOut -> case printModule raw_gTh of
        Nothing ->
            putIfVerbose opts 0 $ "could not translate to Haskell file: " ++ f
        Just d -> writeVerbFile opts f $ shows d "\n"
#endif
    ComptableXml -> if lang == language_name CASL then do
          th2 <- coerceBasicTheory lid CASL "" th
          let Result ds res = computeCompTable i th2
          showDiags opts ds
          case res of
            Just td -> writeVerbFile opts f $ tableXmlStr td
            Nothing -> return ()
        else putIfVerbose opts 0 $ "expected CASL theory for: " ++ f
#ifdef RDFLOGIC
    RDFOut
        | lang == language_name RDF -> do
            th2 <- coerceBasicTheory lid RDF "" th
            let rdftext = shows (RDF.printRDFBasicTheory th2) "\n"
            writeVerbFile opts f rdftext
        | otherwise -> putIfVerbose opts 0 $ "expected RDF theory for: " ++ f
#endif
#ifndef NOOWLLOGIC
    OWLOut ty -> case ty of
      Manchester -> case createOWLTheory raw_gTh of
        Result _ Nothing ->
          putIfVerbose opts 0 $ "expected OWL theory for: " ++ f
        Result ds (Just th2) -> do
            let sy = defSyntax opts
                ms = if null sy then Nothing
                     else Just $ simpleIdToIRI $ mkSimpleId sy
                owltext = shows
                  (printTheory ms OWL2 $ OWL2.prepareBasicTheory th2) "\n"
            showDiags opts ds
            when (null sy)
                $ case parse (OWL2.basicSpec Map.empty >> eof) f owltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f owltext
      _ -> let flp = getFilePath ln in case guess flp GuessIn of
        OWLIn _ -> writeVerbFile opts f =<< convertOWL flp (show ty)
        _ -> putIfVerbose opts 0
          $ "OWL output only supported for owl input types ("
          ++ intercalate ", " (map show plainOwlFormats) ++ ")"
#endif
    CLIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let cltext = shows (CL_AS.exportCLIF th2) "\n"
            case parse (many (CL_Parse.cltext Map.empty) >> eof) f cltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f cltext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    KIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let kiftext = shows (Print_KIF.exportKIF th2) "\n"
            writeVerbFile opts f kiftext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    _ -> return () -- ignore other file types
writeTheory :: [String] -> String -> HetcatsOpts -> FilePath -> GlobalAnnos
  -> G_theory -> LibName -> IRI -> OutType -> IO ()
writeTheory ins nam opts filePrefix ga
  raw_gTh@(G_theory lid _ (ExtSign sign0 _) _ sens0 _) ln i ot =
    let fp = filePrefix ++ "_" ++ i'
        i' = encode (iriToStringShortUnsecure $ setAngles False i)
        f = fp ++ "." ++ show ot
        th = (sign0, toNamedList sens0)
        lang = language_name lid
    in case ot of
    FreeCADOut -> writeFreeCADFile opts filePrefix raw_gTh
    ThyFile -> writeIsaFile opts filePrefix raw_gTh ln i
    DfgFile c -> writeSoftFOL opts f raw_gTh i c 0 "DFG"
    TPTPFile c -> writeSoftFOL opts f raw_gTh i c 1 "TPTP"
    TheoryFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (DG.printTh ga i raw_gTh) "\n"
        else putIfVerbose opts 0 "printing theory delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', sens') = addUniformRestr sign sens
            lse = map (namedSenToSExpr sign') sens'
        unless (null lse) $ writeVerbFile opts (fp ++ ".sexpr")
            $ shows (prettySExpr $ SList lse) "\n"
    SigFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (pretty $ signOf raw_gTh) "\n"
        else putIfVerbose opts 0 "printing signature delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', _sens') = addUniformRestr sign sens
        writeVerbFile opts (f ++ ".sexpr")
          $ shows (prettySExpr $ vseSignToSExpr sign') "\n"
    SymXml -> writeVerbFile opts f $ ppTopElement
           $ ToXml.showSymbolsTh ins nam ga raw_gTh
#ifdef PROGRAMATICA
    HaskellOut -> case printModule raw_gTh of
        Nothing ->
            putIfVerbose opts 0 $ "could not translate to Haskell file: " ++ f
        Just d -> writeVerbFile opts f $ shows d "\n"
#endif
    ComptableXml -> if lang == language_name CASL then do
          th2 <- coerceBasicTheory lid CASL "" th
          let Result ds res = computeCompTable i th2
          showDiags opts ds
          case res of
            Just td -> writeVerbFile opts f $ tableXmlStr td
            Nothing -> return ()
        else putIfVerbose opts 0 $ "expected CASL theory for: " ++ f
#ifdef RDFLOGIC
    RDFOut
        | lang == language_name RDF -> do
            th2 <- coerceBasicTheory lid RDF "" th
            let rdftext = shows (RDF.printRDFBasicTheory th2) "\n"
            writeVerbFile opts f rdftext
        | otherwise -> putIfVerbose opts 0 $ "expected RDF theory for: " ++ f
#endif
#ifndef NOOWLLOGIC
    OWLOut ty -> case ty of
      Manchester -> case createOWLTheory raw_gTh of
        Result _ Nothing ->
          putIfVerbose opts 0 $ "expected OWL theory for: " ++ f
        Result ds (Just th2) -> do
            let sy = defSyntax opts
                ms = if null sy then Nothing
                     else Just $ simpleIdToIRI $ mkSimpleId sy
                owltext = shows
                  (printTheory ms OWL2 $ OWL2.prepareBasicTheory th2) "\n"
            showDiags opts ds
            when (null sy)
                $ case parse (OWL2.basicSpec Map.empty >> eof) f owltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f owltext
      _ -> let flp = getFilePath ln in case guess flp GuessIn of
        OWLIn _ -> writeVerbFile opts f =<< convertOWL flp (show ty)
        _ -> putIfVerbose opts 0
          $ "OWL output only supported for owl input types ("
          ++ intercalate ", " (map show plainOwlFormats) ++ ")"
#endif
    CLIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let cltext = shows (CL_AS.exportCLIF th2) "\n"
            case parse (many (CL_Parse.cltext Map.empty) >> eof) f cltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f cltext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    KIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let kiftext = shows (Print_KIF.exportKIF th2) "\n"
            writeVerbFile opts f kiftext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    _ -> return () -- ignore other file types
writeTheory ins nam opts filePrefix ga
  raw_gTh@(G_theory lid _ (ExtSign sign0 _) _ sens0 _) ln i ot =
    let fp = filePrefix ++ "_" ++ i'
        i' = encode (iriToStringShortUnsecure $ setAngles False i)
        f = fp ++ "." ++ show ot
        th = (sign0, toNamedList sens0)
        lang = language_name lid
    in case ot of
    FreeCADOut -> writeFreeCADFile opts filePrefix raw_gTh
    ThyFile -> writeIsaFile opts filePrefix raw_gTh ln i
    DfgFile c -> writeSoftFOL opts f raw_gTh i c 0 "DFG"
    TPTPFile c -> writeSoftFOL opts f raw_gTh i c 1 "TPTP"
    TheoryFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (DG.printTh ga i raw_gTh) "\n"
        else putIfVerbose opts 0 "printing theory delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', sens') = addUniformRestr sign sens
            lse = map (namedSenToSExpr sign') sens'
        unless (null lse) $ writeVerbFile opts (fp ++ ".sexpr")
            $ shows (prettySExpr $ SList lse) "\n"
    SigFile d -> do
      if null $ show d then
        writeVerbFile opts f $ shows (pretty $ signOf raw_gTh) "\n"
        else putIfVerbose opts 0 "printing signature delta is not implemented"
      when (lang == language_name VSE) $ do
        (sign, sens) <- coerceBasicTheory lid VSE "" th
        let (sign', _sens') = addUniformRestr sign sens
        writeVerbFile opts (f ++ ".sexpr")
          $ shows (prettySExpr $ vseSignToSExpr sign') "\n"
    SymXml -> writeVerbFile opts f $ ppTopElement
           $ ToXml.showSymbolsTh ins nam ga raw_gTh
#ifdef PROGRAMATICA
    HaskellOut -> case printModule raw_gTh of
        Nothing ->
            putIfVerbose opts 0 $ "could not translate to Haskell file: " ++ f
        Just d -> writeVerbFile opts f $ shows d "\n"
#endif
    ComptableXml -> if lang == language_name CASL then do
          th2 <- coerceBasicTheory lid CASL "" th
          let Result ds res = computeCompTable i th2
          showDiags opts ds
          case res of
            Just td -> writeVerbFile opts f $ tableXmlStr td
            Nothing -> return ()
        else putIfVerbose opts 0 $ "expected CASL theory for: " ++ f
#ifdef RDFLOGIC
    RDFOut
        | lang == language_name RDF -> do
            th2 <- coerceBasicTheory lid RDF "" th
            let rdftext = shows (RDF.printRDFBasicTheory th2) "\n"
            writeVerbFile opts f rdftext
        | otherwise -> putIfVerbose opts 0 $ "expected RDF theory for: " ++ f
#endif
#ifndef NOOWLLOGIC
    OWLOut ty -> case ty of
      Manchester -> case createOWLTheory raw_gTh of
        Result _ Nothing ->
          putIfVerbose opts 0 $ "expected OWL theory for: " ++ f
        Result ds (Just th2) -> do
            let sy = defSyntax opts
                ms = if null sy then Nothing
                     else Just $ simpleIdToIRI $ mkSimpleId sy
                owltext = shows
                  (printTheory ms OWL2 $ OWL2.prepareBasicTheory th2) "\n"
            showDiags opts ds
            when (null sy)
                $ case parse (OWL2.basicSpec Map.empty >> eof) f owltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f owltext
      _ -> let flp = getFilePath ln in case guess flp GuessIn of
        OWLIn _ -> writeVerbFile opts f =<< convertOWL flp (show ty)
        _ -> putIfVerbose opts 0
          $ "OWL output only supported for owl input types ("
          ++ intercalate ", " (map show plainOwlFormats) ++ ")"
#endif
    CLIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let cltext = shows (CL_AS.exportCLIF th2) "\n"
            case parse (many (CL_Parse.cltext Map.empty) >> eof) f cltext of
              Left err -> putIfVerbose opts 0 $ show err
              _ -> putIfVerbose opts 3 $ "reparsed: " ++ f
            writeVerbFile opts f cltext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    KIFOut
      | lang == language_name CommonLogic -> do
            (_, th2) <- coerceBasicTheory lid CommonLogic "" th
            let kiftext = shows (Print_KIF.exportKIF th2) "\n"
            writeVerbFile opts f kiftext
      | otherwise -> putIfVerbose opts 0 $ "expected Common Logic theory for: "
                                                                            ++ f
    _ -> return () -- ignore other file types