-
Notifications
You must be signed in to change notification settings - Fork 1
/
UniversalParser.hs
273 lines (221 loc) · 7.38 KB
/
UniversalParser.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
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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module UniversalParser where
import Data.Functor (($>))
import Data.Maybe (fromMaybe)
import Text.Parsec (satisfy, (<|>), (<?>), choice, try, unexpected, lookAhead, char)
import Text.Parsec.String (Parser)
import ParsingHelpers
{-- Universal parser for the following grammar of propositional formulas:
Formula ::= NoFixity | Ors
-- no implicit parenthesis
NoFixity ::= Basic Op Basic | Basic
Basic ::= Nested | Neg
Op ::= ∨ | ∧ | => | <=>
-- with precedence
Ors ::= Ors ∨ Ands | Ands
Ands ::= Ands ∧ Impl | Impl
Impl ::= BiImpl => Impl | BiImpl
BiImpl ::= Neg <=> BiImpl | Neg
-- 'leaf' formulas
Neg ::= ¬ Atom | Atom | ¬ Neg | Nested
Nested ::= ( Formula )
Atom ::= A | .. | Z
Individual grammar productions can be toggled off (at specific nesting depths)
to parse specialized subsets of formulas.
--}
-- types for universal grammar
data FormulaGrammar = WithPrecedence Ors | OfNoFixity NoFixity
deriving Show
data NoFixity = NoFixity Basic Op Basic | OfBasic Basic
deriving Show
data Basic = BasicNested Nested | BasicNeg Neg
deriving Show
data Op = Or | And | Impl | BiImpl
deriving Show
data Ors = Ors Ors Ands | OfAnds Ands
deriving Show
data Ands = Ands Ands Impls | OfImpl Impls
deriving Show
data Impls = Impls BiImpls Impls | OfBiImpl BiImpls
deriving Show
data BiImpls = BiImpls Neg BiImpls | OfNeg Neg
deriving Show
data Neg = NegAtom Atom | OfAtom Atom | Neg Neg | OfNested Nested
deriving Show
newtype Atom = Atom Char
deriving Show
newtype Nested = Nested FormulaGrammar
deriving Show
-- folds over binary operators
foldlOrs :: (a -> Ands -> a) -> a -> Ors -> a
foldlOrs f z (Ors x y) = foldlOrs f z x `f` y
foldlOrs f z (OfAnds x) = f z x
foldlAnds :: (a -> Impls -> a) -> a -> Ands -> a
foldlAnds f z (Ands x y) = foldlAnds f z x `f` y
foldlAnds f z (OfImpl x) = f z x
foldrImpl :: (BiImpls -> a -> a) -> a -> Impls -> a
foldrImpl f z (Impls x y) = f x $ foldrImpl f z y
foldrImpl f z (OfBiImpl x) = f x z
foldrBiImpl :: (Neg -> a -> a) -> a -> BiImpls -> a
foldrBiImpl f z (BiImpls x y) = f x $ foldrBiImpl f z y
foldrBiImpl f z (OfNeg x) = f x z
-- useful pattern synonyms
{-# COMPLETE Ors, NoOrs #-}
pattern NoOrs :: Ands -> Ors
pattern NoOrs x = OfAnds x
{-# COMPLETE Ands, NoAnds #-}
pattern NoAnds :: Impls -> Ands
pattern NoAnds x = OfImpl x
{-# COMPLETE Impls, NoImpls #-}
pattern NoImpls :: BiImpls -> Impls
pattern NoImpls x = OfBiImpl x
{-# COMPLETE BiImpls, NoBiImpls #-}
pattern NoBiImpls :: Neg -> BiImpls
pattern NoBiImpls x = OfNeg x
{-# COMPLETE Impls, NoImplBiImpl, NoArrows #-}
pattern NoImplBiImpl :: Impls
pattern NoImplBiImpl <- NoImpls (BiImpls _ _)
pattern NoArrows :: Neg -> Impls
pattern NoArrows x = NoImpls (NoBiImpls x)
{-# COMPLETE Ors, SkipLevel #-}
pattern SkipLevel :: FormulaGrammar -> Ors
pattern SkipLevel x = NoOrs (NoAnds (NoArrows (OfNested (Nested x))))
-- parser configuration
data LevelSpec = LevelSpec
{ allowOr :: Bool
, allowAnd :: Bool
, allowNegation :: AllowNegation
, allowAtomicProps :: Bool
, allowImplication :: Bool
, allowBiImplication :: Bool
, strictParens :: Bool -- strict parenthesis will trigger excessive nesting and thereby level changes!!!
, allowSilentNesting :: Bool -- DANGER!!!
, nextLevelSpec :: Maybe LevelSpec
}
data AllowNegation = Nowhere | LiteralsOnly | Everywhere
-- parser for operations
orParser :: Parser ()
orParser =
keyword "or"
<|> keyword "oder" {- german -}
<|> tokenSymbol "∨" <|> tokenSymbol "\\/"
<?> "Disjunction"
andParser :: Parser ()
andParser =
keyword "and"
<|> keyword "und" {- german -}
<|> tokenSymbol "∧" <|> tokenSymbol "/\\"
<?> "Conjunction"
implicationParser :: Parser ()
implicationParser = tokenSymbol "=>" <?> "Implication"
biImplicationParser :: Parser ()
biImplicationParser =
tokenSymbol "<=>"
<|> keyword "iff" <|> keyword "gdw"
<?> "BiImplication"
negationParser :: Parser ()
negationParser =
keyword "not" <|> keyword "nicht"
<|> tokenSymbol "¬" <|> tokenSymbol "~" <|> tokenSymbol "-"
<?> "Negation"
atomParser :: Parser Char
atomParser = token (satisfy (`elem` ['A'..'Z'])) <?> "atomic Proposition"
-- the universal parser
formula :: LevelSpec -> Parser FormulaGrammar
formula LevelSpec{..}
| strictParens = OfNoFixity <$> noFixity
| otherwise = WithPrecedence <$> ors
where
noFixity :: Parser NoFixity
noFixity = do
x <- basic
NoFixity x <$> anyBinaryOp <*> basic <|> pure (OfBasic x)
anyBinaryOp :: Parser Op
anyBinaryOp = _noNested *> chooseBinary <* _noSecondOp
where
chooseBinary :: Parser Op
chooseBinary = choice
( [ Or <$ orParser | allowOr ]
++ [ And <$ andParser | allowAnd ]
++ [ Impl <$ implicationParser | allowImplication ]
++ [ BiImpl <$ biImplicationParser | allowBiImplication ]
)
_noNested :: Parser () -- catch the A (B and C) error pattern
_noNested = (do
lookAhead $ char '('
_ <- try nested
unexpected "formula"
fail "maybe there is an operator missing")
<|> pure ()
_noSecondOp :: Parser ()
_noSecondOp = (do
op <- try chooseBinary
unexpected $ "second operator: " ++ case op of
Or -> "Disjunction"
And -> "Conjunction"
Impl -> "Implication"
BiImpl -> "Bi-Implication")
<|> pure ()
basic :: Parser Basic
basic = BasicNested <$> nested <|> BasicNeg <$> neg
ors :: Parser Ors
ors
| allowOr = infixl1 OfAnds ands (orParser $> Ors)
| otherwise = OfAnds <$> ands
ands :: Parser Ands
ands
| allowAnd = infixl1 OfImpl impl (andParser $> Ands)
| otherwise = OfImpl <$> impl
impl :: Parser Impls
impl
| allowImplication = infixr1 OfBiImpl biImpl (implicationParser $> Impls)
| otherwise = OfBiImpl <$> biImpl
biImpl :: Parser BiImpls
biImpl
| allowBiImplication = infixr1 OfNeg neg (biImplicationParser $> BiImpls)
| otherwise = OfNeg <$> neg
neg :: Parser Neg
neg = case allowNegation of
Everywhere ->
plainNested
<|> plainAtom
<|> negationParser *> nextNeg
where nextNeg = NegAtom <$> atom <|> Neg . OfNested <$> nested <|> Neg <$> neg
LiteralsOnly ->
plainNested
<|> plainAtom
<|> negationParser $> NegAtom <*> atom
Nowhere -> plainNested <|> plainAtom
where
plainAtom = OfAtom <$> atom
plainNested = OfNested <$> nested
atom :: Parser Atom
atom
| allowAtomicProps = Atom <$> atomParser
| otherwise = fail "no atomic propositions allowed at this level"
nested :: Parser Nested
nested
| allowSilentNesting = Nested <$> silentNested <|> Nested <$> innerNested
| otherwise = Nested <$> innerNested
where
innerNested =
maybe
(fail "term structure does not allow nesting at this point")
(parens . formula)
nextLevelSpec
silentNested =
maybe
(fail "term structure does not allow nesting at this point")
formula
nextLevelSpec
-- type class for parseable types
class FromGrammar a where
topLevelSpec :: LevelSpec
fromGrammar :: FormulaGrammar -> Maybe a
formulaParser :: forall a. FromGrammar a => Parser a
formulaParser =
fromMaybe (error "contradiction between parser rules and fromGrammar") . fromGrammar <$> formula (topLevelSpec @a)