Add command-line interface ./match (adapted from POPL 2012 version).
[match/match.git] / program / Main.hs
CommitLineData
bbabbd01
MM
1import PMInstance
2import PMDefaults
3import ProposalMatcher
4import System.IO
5import Data.Array.IArray
6import Data.Array.Unboxed
7import ArrayStuff
8import Text.CSV
9
10-- Command-line interface with simple tab-separated input/output formats.
11-- ./match <example.in
12
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
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
22 Left pe -> Left pe
23 Right ll -> Right $ map (map swapTabCommaIn) ll
24printTSV ll = addTrailingNewline $ removeQuotes $ swapTabCommaIn $ printCSV $ map (map swapTabCommaIn) ll
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
30 let loadList = head inll
31 let numRvrs = length loadList
32 let loadA = listArray (0, numRvrs-1) (map read loadList)
33 let numProps = length (tail inll)
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
37 let prefA = funcArray ((0,0), (numRvrs-1,numProps-1)) (\(i,j) -> read (pxarr ! (j, i)))
38 let theInst = PMInstance numRvrs numProps loadA prefA
39 let PMatching theMatching = doMatching pmDefaults theInst
40 hPutStr stdout $ printTSV $ map (\(i, j) -> map show [i, j]) theMatching