forked from jwaldmann/haskell-tpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compressor.hs
80 lines (66 loc) · 1.96 KB
/
Compressor.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
67
68
69
70
71
72
73
74
75
76
77
78
79
{-# language DoAndIfThenElse #-}
import TPDB.Compress (compress)
import TPDB.DP ( dp, Marked (Auxiliary) )
import TPDB.Data
import TPDB.XTC.Read ( readProblems )
import TPDB.Plain.Write ( )
import TPDB.Pretty ( pretty )
import Text.PrettyPrint.HughesPJ
( text, ($$), nest, vcat )
import System.Environment
import System.IO
import System.Directory
import Control.Monad ( guard, forM )
import Data.List ( isSuffixOf, isPrefixOf )
import qualified Data.Set as S
-- | give file/dir name(s) on cmd line.
-- recursively collect all *.xml files,
-- and apply TRS compression.
main = do
hSetBuffering stdout LineBuffering
args <- getArgs
ss <- collect args
putStrLn "action"
forM ( map dp ss ) $ \ s -> do
print $ text "original TRS" </> pretty s
let ( com, rules ) = compress supply s
print $ text "compressed TRS" </> vcat
( pretty com : map pretty rules )
print $ judge s
p </> q = p $$ nest 4 q
judge sys =
let csys = compress supply sys
in ( cost_trs sys , cost_comp csys )
supply = do
i <- [ 0 .. ]
return $ Auxiliary $ mknullary $ "c" ++ show i
cost_comp ( s, sub ) =
cost_trs s + cost_sub sub
cost_sub sub = sum $ do
((f, af), Just (g, k, h, ca)) <- sub
return ca
cost_trs s = sum $ do
u <- rules s
t <- [ lhs u, rhs u ]
return $ cost_term t
cost_term t = sum $ do
(p,s) <- positions t
return $ multiplications s
multiplications t = case t of
Node f xs -> sum $ do
x @ Node {} <- xs
return $ S.size $ vars x
_ -> 0
collect fs = do
let special d = isPrefixOf "." d
fss <- forM fs $ \ f -> do
d <- doesDirectoryExist f
if d
then do
fs <- getDirectoryContents f
collect $ map ( ( f ++ "/" ) ++ )
$ filter (not . special) fs
else if isSuffixOf ".xml" f
then fmap (map trs) $ readProblems f
else return []
return $ concat fss