-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUI.hs
232 lines (195 loc) · 8.24 KB
/
UI.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
module UI where
import Data.List (intersperse)
import Data.Char (toLower)
import Control.Monad.Trans.Class
import Control.Monad.Trans.State.Strict
import System.Console.Haskeline
import Text.Parsec.Prim (parse)
import F24
import F24File
import Defs
import LTL24()
import Spec
import SpecFile
import qualified Parsers as P
prompt = "LTL24$ "
data Configuration = Configuration {
cores :: Int
}
data Environment = Environment {
config :: Configuration,
games :: [Game],
specs :: [Spec]
}
instance Show Environment where
show env = let join :: [String] -> String
join = \items -> concat $ intersperse ", " items
desc :: (a -> String) -> String -> [a] -> String
desc _ n [] = "0 " ++ n
desc f n is | length is < 5 = (show (length is)) ++ " " ++ n ++ ": " ++ (join $ map f is)
desc f n is | length is >= 5 = desc f n (take 4 is) ++ ", ..."
in desc (show . gid) "game(s)" (games env) ++ ".\n" ++
desc specName "spec(s)" (specs env) ++ "."
getGame :: Environment -> Int -> Maybe Game
getGame env gameId = let f :: [Game] -> Maybe Game
f [] = Nothing
f (g:gs) = if gid g == gameId then Just g else f gs
in f (games env)
addGame :: Game -> Environment -> Environment
addGame game env = env {games = games env ++ [game]}
dropGame :: Game -> Environment -> Environment
dropGame game env = env {games = [gg | gg <- games env, gg /= game]}
getSpec :: Environment -> String -> Maybe Spec
getSpec env nSpec = let f :: [Spec] -> Maybe Spec
f [] = Nothing
f (s:ss) = if specName s == nSpec then Just s else f ss
in f (specs env)
addSpec :: Spec -> Environment -> Environment
addSpec spec env = env {specs = specs env ++ [spec]}
dropSpec :: Spec -> Environment -> Environment
dropSpec spec env = env {specs = [ss | ss <- specs env, ss /= spec]}
defaultEnv :: Environment
defaultEnv = Environment{
config = Configuration{
cores = 1
},
games = [],
specs = []
}
type ProgramState = InputT (StateT Environment IO) ()
data Command = Command {
commandName :: String,
helpText :: String,
runCommand :: [String] -> ProgramState
}
failCmd :: Command -> ProgramState
failCmd cmd = outputStrLn ("Invalid command syntax. " ++
"Say \"help " ++ commandName cmd ++ "\" for help.")
commands :: [Command]
commands = [cmdQuit, cmdHelp, cmdAbout, cmdLoad, cmdStatus, cmdVerify, cmdDrop]
getCommand :: String -> Maybe Command
getCommand name = lookup name (zip (map commandName commands) commands)
-- The main program loop
loop :: ProgramState
loop = do input <- getInputLine prompt
case input of
Nothing -> runCommand cmdQuit [] -- Ctrl+D
Just "" -> loop -- Return
Just sth -> let name = head (words sth)
params = tail (words sth)
msg = "Unknown command \"" ++ name ++ "\". " ++
"Say \"help\" for the list of " ++
"available commands."
in if name == "quit" then runCommand cmdQuit params
else case getCommand name of
Nothing -> outputStrLn msg >> loop
Just cmd -> runCommand cmd params >> loop
cmdQuit :: Command
cmdQuit = Command {
commandName = "quit",
helpText = "quit -- exit the program.\n" ++
"USAGE: quit",
runCommand = runQuit
}
runQuit :: [String] -> ProgramState
runQuit _ = do input <- getInputLine "Really quit [y/n]? "
case maybe Nothing (Just . (map toLower)) input of
Just "y" -> outputStrLn "Bye." >> return ()
Nothing -> runQuit []
otherwise -> loop
cmdHelp :: Command
cmdHelp = Command {
commandName = "help",
helpText = "help -- display information on available commands.\n" ++
"USAGE: help\n" ++
" help <command name>",
runCommand = runHelp
}
runHelp :: [String] -> ProgramState
runHelp [] = let cmdList = concat $ intersperse ", " (map commandName commands)
in outputStrLn ("Available commands: " ++ cmdList ++ ".")
runHelp (sth:_) = case getCommand sth of
Nothing -> outputStrLn ("Unknown command " ++ show sth ++ ".")
Just cmd -> outputStrLn $ helpText cmd
cmdAbout :: Command
cmdAbout = Command {
commandName = "about",
helpText = "about -- display information about the program.\n" ++
"USAGE: about",
runCommand = runAbout
}
runAbout :: [String] -> ProgramState
runAbout _ = do outputStrLn "LTL24 data querying and verification system, version 0.0.1."
cmdLoad :: Command
cmdLoad = Command {
commandName = "load",
helpText = "load -- add games or specs to the active environment.\n" ++
"USAGE: load [game | spec] <filename>\n" ++
" load spec inline <name>:<formula>",
runCommand = runLoad
}
-- FIXME: Check if game or spec being added already exists
runLoad :: [String] -> ProgramState
runLoad ("game":rest) = do game <- lift (lift (loadGameFromFile (head rest)))
lift (modify (addGame game))
runCommand cmdStatus []
runLoad ("spec":("inline":rest)) = do case parse P.spec "" (unwords rest) of
Left _ -> outputStrLn "Syntax error."
Right sp -> do lift $ modify (addSpec sp)
runCommand cmdStatus []
runLoad ("spec":rest) = do ss <- lift $ lift $ loadSpecsFromFile (head rest)
let f :: [Spec] -> ProgramState
f [] = runCommand cmdStatus []
f (s:tt) = (lift $ modify (addSpec s)) >> f tt
f ss
runLoad _ = failCmd cmdLoad
cmdStatus :: Command
cmdStatus = Command {
commandName = "status",
helpText = "status -- display the state of the active environment.\n" ++
"USAGE: status",
runCommand = runStatus
}
runStatus :: [String] -> ProgramState
runStatus _ = do env <- lift get
outputStrLn (show env)
cmdVerify :: Command
cmdVerify = Command {
commandName = "verify",
helpText = "verify -- check if the games satisfy the specs.\n" ++
"USAGE: verify",
runCommand = runVerify
}
runVerify :: [String] -> ProgramState
runVerify _ = do env <- lift get
let cg :: Game -> [Spec] -> ProgramState
cg g [] = return ()
cg g (s:ss) = let passed = sat (specFormula s) (events g)
msg = if passed then "passed" else "failed"
in outputStrLn ((specName s) ++ ": " ++ msg) >> cg g ss
let cgs :: [Game] -> [Spec] -> ProgramState
cgs [] _ = return ()
cgs (g:gs) ss = do outputStrLn ("Verifying game " ++ (show $ gid g) ++ ".")
cg g ss
cgs gs ss
cgs (games env) (specs env)
cmdDrop :: Command
cmdDrop = Command {
commandName = "drop",
helpText = "drop -- remove a game or a spec from the active environment.\n" ++
"USAGE: drop [game | spec] <identifier>",
runCommand = runDrop
}
-- FIXME: Modify to handle multiple game and spec identifiers
runDrop :: [String] -> ProgramState
runDrop ("game":ids) = do env <- lift get
case getGame env (read (head ids)) of
Nothing -> outputStrLn "No such game."
Just g -> lift $ modify (dropGame g)
runCommand cmdStatus []
runDrop ("spec":ids) = do env <- lift get
case getSpec env (head ids) of
Nothing -> outputStrLn "No such spec." >> return ()
Just s -> lift $ modify (dropSpec s)
runCommand cmdStatus []
runDrop _ = failCmd cmdDrop