-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
60 lines (56 loc) · 1.94 KB
/
Main.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
{-# LANGUAGE LambdaCase #-}
module Main where
import System.Environment
import System.IO
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.List
import Data.Foldable
import Data.Maybe
import Tseitin
import Clause
prettyPrint :: Handle -> EncoderState -> IO ()
prettyPrint handle EncoderState{formulaNames=names, cnfRepr=cnf} = do
originalCount <- sequence
[ case key of
FVar name -> hPutStrLn handle ("c " ++ name ++ " = " ++ show value) >> return 1
_ -> return 0
| (key, value) <- Map.toList names
]
let totalCount = Map.size names
hPutStrLn handle $ "c $root = " ++ show totalCount
hPutStrLn handle $ "p cnf " ++ show totalCount ++ ' ' : show (length cnf)
sequence_
[ sequence_ [ hPutStr handle (show item ++ " ") | item <- Set.toList clause] >> hPutStrLn handle "0"
| Clause clause <- toList cnf
]
main :: IO ()
main = do
args <- getArgs
let (options, arguments) = partition
(\case '-':'-':rest -> True
_ -> False)
args
implOpt = "--impl" `elem` options
(input, output) = case arguments of
[i, o] -> (Just i, Just o)
[i] -> (Just i, Nothing)
[] -> (Nothing, Nothing)
_ -> error "Invalid format"
inputHandle <- traverse (`openFile` ReadMode) input
contents <- maybe T.getContents T.hGetContents inputHandle
let result = do
f <- runParse (fromMaybe "stdin" input) contents
return $ runEncode f implOpt
traverse_ hClose inputHandle
case result of
Right result -> do
case output of
Nothing -> prettyPrint stdout result
Just o -> do
handle <- openFile o WriteMode
prettyPrint handle result
hClose handle
Left e -> print e