Merge branch 'master' into popl2012
[match/match.git] / program / Main.hs
CommitLineData
56b565b1
MM
1import PMInstance
2import PMDefaults
3import ProposalMatcher
4import System.IO
5import Data.Array.IArray
578d7d98 6import Data.Array.Unboxed
56b565b1
MM
7import ArrayStuff
8import Text.CSV
9
bbabbd01
MM
10-- Command-line interface with simple tab-separated input/output formats.
11-- ./match <example.in
12
56b565b1
MM
13-- pretty silly but it does the job
14swapTabCommaIn s = map (\c -> if c == '\t' then ',' else if c == ',' then '\t' else c) s
15removeQuotes s = filter (\c -> not (c == '"')) s
bbabbd01
MM
16
17-- Some versions of Text.CSV do not use or accept a trailing newline; compensate for that.
18removeTrailingNewline s = if not (null s) && last s == '\n' then init s else s
19addTrailingNewline s = if not (null s) && last s /= '\n' then s ++ ['\n'] else s
20
21parseTSV fname str = case parseCSV fname (swapTabCommaIn $ removeTrailingNewline str) of
56b565b1
MM
22 Left pe -> Left pe
23 Right ll -> Right $ map (map swapTabCommaIn) ll
bbabbd01 24printTSV ll = addTrailingNewline $ removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll
56b565b1
MM
25
26main = do
27 incsv <- hGetContents stdin
28 -- handle errors another day, or let the platform do it
29 let Right inll = parseTSV "standard input" incsv
578d7d98 30 let loadList = tail (head inll)
56b565b1
MM
31 let numRvrs = length loadList
32 let loadA = listArray (0, numRvrs-1) (map read loadList)
33 let numProps = length (tail inll) `div` 2
34 -- explicit type on the next line appears to be necessary
35 let pxarr = listOfListsToArray2D (tail inll) :: Array (Int,Int) String
36 -- careful, we end up transposing the array in here
578d7d98
MM
37 let prefFixA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) ->
38 let
39 cell = pxarr ! (2*j, i+1)
40 (fix, pstr) = if last cell == '*' then (True, init cell) else (False, cell)
41 pref = read pstr
42 in (pref, fix)) :: Array (Int,Int) (Wt,Bool)
43 let prefA = amap2 fst prefFixA; fixA = amap2 snd prefFixA
44 let expA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read $ pxarr ! (2*j+1, i+1))
45 let pnrA = funcArray (0, numProps-1) (\j -> read $ pxarr ! (2*j, 0))
46 let theInst = PMInstance numRvrs numProps loadA prefA expA fixA pnrA
56b565b1
MM
47 let PMatching theMatching = doMatching pmDefaults theInst
48 hPutStr stdout $ printTSV $ map (\(i, j) -> map show [i, j]) theMatching