-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathParseProblem.hs
More file actions
294 lines (245 loc) · 6.76 KB
/
Copy pathParseProblem.hs
File metadata and controls
294 lines (245 loc) · 6.76 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
{-# LANGUAGE MonadFailDesugaring #-}
module ParseProblem where
import System.IO.Error as E
import Data.List
import Data.Char
import Parsek as P
-------------------------------------------------------------------------
-- types
type Name
= String
data Form
= Atom Name
| Form :&: Form
| Form :|: Form
| Form :=>: Form
| Form :<=>: Form
| TRUE
| FALSE
deriving ( Eq, Ord )
instance Show Form where
show (Atom a) = a
show (p :&: q) = "(" ++ show p ++ " & " ++ show q ++ ")"
show (p :|: q) = "(" ++ show p ++ " | " ++ show q ++ ")"
show (p :=>: q) = "(" ++ show p ++ " => " ++ show q ++ ")"
show (p :<=>: q) = "(" ++ show p ++ " <=> " ++ show q ++ ")"
show TRUE = "$true"
show FALSE = "$false"
nt :: Form -> Form
nt p = p :=>: FALSE
data Input a
= Input Name Role a
deriving ( Eq, Ord )
instance Show a => Show (Input a) where
show (Input name role x) =
"fof(" ++ name ++ ", " ++ show role ++ ", " ++ show x ++ " )."
data Role
= Fact
| Conjecture
deriving ( Eq, Ord )
instance Show Role where
show Fact = "axiom"
show Conjecture = "conjecture"
oneForm :: [Input Form] -> Form
oneForm fs
= case un conjs of
[conj] ->
case un axs of
[] -> conj
as -> foldr1 (:&:) as :=>: conj
_ -> error "Need exactly one conjecture!"
where
(axs,conjs) = partition (\ (Input _ r _) -> r == Fact) fs
un xs = [ x | Input _ _ x <- xs ]
showFCubeProblem :: [Input Form] -> String
showFCubeProblem fs =
"intDecide(" ++ showFCube (oneForm fs) ++ ",X,'generated by intuit')."
showFCube :: Form -> String
showFCube = ($[]) . go
where
go (p :=>: FALSE) = text "non(" . go p . text ")"
go (Atom a) = text a
go (p :&: q) = text "and(" . go p . text ", " . go q . text ")"
go (p :|: q) = text "or(" . go p . text ", " . go q . text ")"
go (p :=>: q) = text "im(" . go p . text ", " . go q . text ")"
go (p :<=>: q) = text "equiv(" . go p . text ", " . go q . text ")"
go TRUE = text "im(tmp,tmp)" -- ick !
go FALSE = text "non(im(tmp,tmp))" -- ick !
showIntHistGCProblem :: [Input Form] -> String
showIntHistGCProblem = showIntHistGC . oneForm
showIntHistGC :: Form -> String
showIntHistGC = ($[]) . go
where
go (p :=>: FALSE) = text "~(" . go p . text ")"
go (Atom a) = text a
go (p :&: q) = text "(" . go p . text " & " . go q . text ")"
go (p :|: q) = text "(" . go p . text " | " . go q . text ")"
go (p :=>: q) = text "(" . go p . text " => " . go q . text ")"
go (p :<=>: q) = text "(" . go p . text " <=> " . go q . text ")"
go TRUE = text "(tmp => tmp)" -- ick !
go FALSE = text "~ (tmp => tmp)" -- ick !
text s = (s ++)
-------------------------------------------------------------------------
-- reading
readProblem :: FilePath -> IO (Maybe [Input Form])
readProblem name =
do ms <- tryIOError (readFile name)
case ms of
Left _ ->
do putStrLn ("*** READ ERROR: " ++ show name)
return Nothing
Right s ->
case parseP s of
Left xs ->
do putStrLn ("*** PARSE ERROR: " ++ show name)
putStr (unlines xs)
return Nothing
Right fs ->
do return (Just fs)
-------------------------------------------------------------------------
-- parsing
type P a = Parser Char a
-- white space
white :: P ()
white =
do munch isSpace
option () $
do char '%' <?> ""
many (satisfy (/= '\n'))
char '\n'
white
<|>
do char '/' <?> ""
char '*'
s <- P.look
let body ('*':'/':s) =
do anyChar
anyChar
return ()
body (_:s) =
do anyChar
body s
body [] =
do return ()
body s
white
token :: String -> P String
token s =
do white
string s
<?> show s
name :: P Name
name =
do white
munch1 isIdfChar
<?> "name"
where
isIdfChar c = isAlphaNum c || c == '_'
parens :: P a -> P a
parens = between (token "(") (token ")")
bracks :: P a -> P a
bracks = between (token "[") (token "]")
-- atoms
atom :: P Form
atom =
do token "$false"
return FALSE
<|>
do token "$true"
return TRUE
<|>
do a <- name
return (Atom a)
<?> "atom"
-- forms
form :: P Form
form =
do foper ops
<?> "formula"
where
ops = [ ("<=>", \x y -> x :<=>: y)
, ("<~>", \x y -> nt (x :<=>: y))
, ("=>", \x y -> x :=>: y)
, ("<=", \x y -> y :=>: x)
, ("|", \x y -> x :|: y)
, ("~|", \x y -> nt (x :|: y))
, ("&", \x y -> x :&: y)
, ("~&", \x y -> nt (x :&: y))
]
foper :: [(String, Form->Form->Form)] -> P Form
foper [] = funit
foper ops@((sym,fun):ops') =
do a <- foper ops'
option a $
do token sym
b <- foper ops
return (a `fun` b)
funit :: P Form
funit =
do parens form
<|>
do atom
<|>
do token "~"
nt `fmap` funit
<?> "formula unit"
-- formulas and clauses
formula :: P (Input Form)
formula =
do token "fof"
x <- parens $
do white
s <- name <|> (token (show "") >> return "")
token ","
white
(st,t) <- ptype
token ","
f <- form
option () (do token ","
let junk =
do munch (`notElem` "()")
option () (do token "("; junk; token ")"; junk)
in junk)
return (Input s t f)
token "."
return x
where
ptype = choice
[ do token s
return (s,t)
| (s,t) <- typeList
]
typeList =
[ ("axiom", Fact) -- ..
, ("theorem", Fact) -- I see no reason to distinguish these
, ("lemma", Fact) -- ..
, ("hypothesis", Fact) -- ..
, ("definition", Fact) -- TODO: treat this one specially
, ("conjecture", Conjecture)
, ("question", Conjecture)
]
-- includes
problem :: P [Input Form]
problem =
do xs <- many formula
white
return xs
parseP :: String -> Either [String] [Input Form]
parseP s =
case parse problem completeResultsWithLine s of
Left (n, exp, unexp) ->
Left $
[ "On line: " ++ show n ] ++
[ "Unexpected: " ++ commas "and" unexp | not (null unexp) ] ++
[ "Expected: " ++ commas "or" exp | not (null exp) ]
Right [x] ->
Right x
Right _ ->
Left $
[ "Internal error: Ambiguous parse!"
, "Please report this as a bug in the parser."
]
where
commas op = concat . intersperse (", " ++ op ++ " ")
-------------------------------------------------------------------------
-- the end.