e3dbfff4610e573bd418fb0f6c8a89278e42dc4e
[match/match.git] / program / Main.hs
1 import PMInstance
2 import PMDefaults
3 import ProposalMatcher
4 import System.IO
5 import Data.Array.IArray
6 import ArrayStuff
7 import Text.CSV
8
9 -- pretty silly but it does the job
10 swapTabCommaIn s = map (\c -> if c == '\t' then ',' else if c == ',' then '\t' else c) s
11 removeQuotes s = filter (\c -> not (c == '"')) s
12 parseTSV fname str = case parseCSV fname (swapTabCommaIn str) of
13         Left pe -> Left pe
14         Right ll -> Right $ map (map swapTabCommaIn) ll
15 printTSV ll = removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll
16
17 main = do
18         incsv <- hGetContents stdin
19         -- handle errors another day, or let the platform do it
20         let Right inll = parseTSV "standard input" incsv
21         let loadList = head inll
22         let numRvrs = length loadList
23         let loadA = listArray (0, numRvrs-1) (map read loadList)
24         let numProps = length (tail inll) `div` 2
25         -- explicit type on the next line appears to be necessary
26         let pxarr = listOfListsToArray2D (tail inll) :: Array (Int,Int) String
27         -- careful, we end up transposing the array in here
28         let prefA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read $ pxarr ! (2*j, i))
29         let expA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read $ pxarr ! (2*j+1, i))
30         let theInst = PMInstance numRvrs numProps loadA prefA expA
31         let PMatching theMatching = doMatching pmDefaults theInst
32         hPutStr stdout $ printTSV $ map (\(i, j) -> map show [i, j]) theMatching