-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadCNF.hs
67 lines (46 loc) · 1.85 KB
/
ReadCNF.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
61
62
63
64
65
66
{- |
Module : <File name or $Header$ to be replaced automatically>
Description : <optional short text displayed on contents page>
Copyright : (c) <Authors or Affiliations>
License : <license>
Maintainer : <email>
Stability : unstable | experimental | provisional | stable | frozen
Portability : portable | non-portable (<reason>)
<module description starting at first column>
-}
module ReadCNF where
import Data.List (isPrefixOf)
import Data.List.Split (splitOn)
import Data.String (words)
parse :: [Char] -> [[Int]]
{- ^
-- Takes a DIMACS file as an input string and generates a [[Int]]
-- CNF representation.
Parameters:
cnf - DIMACS .cnf file input directly from a file as a string.
Returns:
[[Int]] - Double list CNF.
-}
parse cnf = makeCNF $ stripComments $ splitOn "\n" cnf
makeCNF :: [[Char]] -> [[Int]]
-- ^ Essentially a helper for parse. Process [[Char]] CNF instance.
makeCNF cnf = map stringsToInts $ dropEmpties $
map dropZero $ map words $ stripProblem cnf
stringsToInts :: [String] -> [Int]
-- ^ Converts number strings to Ints in each clause.
stringsToInts clause = map (read::String->Int) clause
dropEmpties :: Eq t => [[t]] -> [[t]]
-- ^ Removes any empty lines from preprocessed instance.
dropEmpties cnf = filter (/= []) cnf
dropZero :: [[Char]] -> [[Char]]
-- ^ Removes 0's from the end of clauses.
dropZero clause = filter (/= "0") clause
getProblem :: [[Char]] -> [Char]
-- ^ Processes problem information from DIMACS file (currently unused).
getProblem cnf = head $ filter (isPrefixOf "p") cnf
stripProblem :: [[Char]] -> [[Char]]
-- ^ Removes problem line "p cnf". Information is acquired parsing cnf.
stripProblem cnf = filter (not . isPrefixOf "p") cnf
stripComments :: [[Char]] -> [[Char]]
-- ^ Removes any lines starting with a 'c'
stripComments cnf = filter (not . isPrefixOf "c") cnf