-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathParse_AS_Basic.hs
171 lines (144 loc) · 5.63 KB
/
Parse_AS_Basic.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
{-# OPTIONS -fallow-undecidable-instances #-}
{- |
Module : $Header$
Description : Instance of class Logic for propositional logic
Copyright : (c) Dominik Luecke, Uni Bremen 2007
License : similar to LGPL, see HetCATS/LICENSE.txt or LIZENZ.txt
Maintainer : luecke@tzi.de
Stability : experimental
Portability : portable
Parser for abstract syntax for propositional logic
Ref.
<http://en.wikipedia.org/wiki/Propositional_logic>
Acknowledgements:
I'd like to thank Christian Maeder for his help and advice.
-}
module Propositional.Parse_AS_Basic
(
basicSpec -- Parser for basic specs
)
where
import qualified Common.AnnoState as AnnoState
import qualified Common.AS_Annotation as Annotation
import qualified Common.Id as Id
import qualified Common.Keywords as Keywords
import Common.Lexer as Lexer
import qualified Propositional.AS_BASIC_Propositional as AS_BASIC
import qualified Common.AS_Annotation as AS_Anno
import Text.ParserCombinators.Parsec
propKeywords :: [String]
propKeywords = [
Keywords.propS
,Keywords.notS
,Keywords.trueS
,Keywords.falseS
]
-- | Toplevel parser for basic specs
basicSpec :: AnnoState.AParser st AS_BASIC.BASIC_SPEC
basicSpec = (fmap AS_BASIC.Basic_spec $ AnnoState.annosParser $ parseBasicItems)
<|> (Lexer.oBraceT >> Lexer.cBraceT >> return (AS_BASIC.Basic_spec []))
-- | Parser for basic items
parseBasicItems :: AnnoState.AParser st AS_BASIC.BASIC_ITEMS
parseBasicItems =
parsePredDecl
<|>
parseAxItems
-- | parser for predicate declarations
parsePredDecl :: AnnoState.AParser st AS_BASIC.BASIC_ITEMS
parsePredDecl = fmap AS_BASIC.Pred_decl predItem
-- | parser for Axiom_items
parseAxItems :: AnnoState.AParser st AS_BASIC.BASIC_ITEMS
parseAxItems =
do d <- AnnoState.dotT
(fs, ds) <- aFormula `Lexer.separatedBy` AnnoState.dotT
(_, an) <- AnnoState.optSemi
let _ = Id.catPos (d:ds)
ns = init fs ++ [AS_Anno.appendAnno (last fs) an]
return $ AS_BASIC.Axiom_items ns
-- | Any word to token
propId :: GenParser Char st Id.Token
propId = Lexer.pToken $ Lexer.reserved propKeywords Lexer.scanAnyWords
-- | parser for predicates = propositions
predItem :: AnnoState.AParser st AS_BASIC.PRED_ITEM
predItem =
do
v <- AnnoState.asKey (Keywords.propS++Keywords.sS) <|>
AnnoState.asKey Keywords.propS
(ps, cs) <- propId `Lexer.separatedBy` AnnoState.anComma
return $ AS_BASIC.Pred_item ps $ Id.catPos(v : cs)
-- | Parser for implies @=>@
implKey :: AnnoState.AParser st Id.Token
implKey = AnnoState.asKey Keywords.implS
-- | Parser for and @\/\ @
andKey :: AnnoState.AParser st Id.Token
andKey = AnnoState.asKey Keywords.lAnd
-- | Parser for or @\\\/@
orKey :: AnnoState.AParser st Id.Token
orKey = AnnoState.asKey Keywords.lOr
-- | Parser for true
trueKey :: AnnoState.AParser st Id.Token
trueKey = AnnoState.asKey Keywords.trueS
-- | Parser for false
falseKey :: AnnoState.AParser st Id.Token
falseKey = AnnoState.asKey Keywords.falseS
-- | Parser for not
notKey :: AnnoState.AParser st Id.Token
notKey = AnnoState.asKey Keywords.notS
-- | Parser for negation
negKey :: AnnoState.AParser st Id.Token
negKey = AnnoState.asKey Keywords.negS
-- | Parser for equivalence @<=>@
equivKey :: AnnoState.AParser st Id.Token
equivKey = AnnoState.asKey Keywords.equivS
-- | Parser for primitive formulae
primFormula :: AnnoState.AParser st AS_BASIC.FORMULA
primFormula =
do c <- trueKey
return (AS_BASIC.True_atom $ Id.tokPos c)
<|>
do c <- falseKey
return (AS_BASIC.False_atom $ Id.tokPos c)
<|>
do c <- notKey <|> negKey <?> "\"not\""
k <- primFormula
return (AS_BASIC.Negation k $ Id.tokPos c)
<|> parenFormula
<|> fmap AS_BASIC.Predication propId
-- | Parser for formulae containing 'and' and 'or'
andOrFormula :: AnnoState.AParser st AS_BASIC.FORMULA
andOrFormula =
do f <- primFormula
do c <- andKey
(fs, ps) <- primFormula `Lexer.separatedBy` andKey
return (AS_BASIC.Conjunction (f:fs) (Id.catPos (c:ps)))
<|>
do c <- orKey
(fs, ps) <- primFormula `Lexer.separatedBy` orKey
return (AS_BASIC.Disjunction (f:fs) (Id.catPos (c:ps)))
<|> return f
-- | Parser for formulae with implications
impFormula :: AnnoState.AParser st AS_BASIC.FORMULA
impFormula =
do f <- andOrFormula
do c <- implKey
(fs, ps) <- andOrFormula `Lexer.separatedBy` implKey
return (makeImpl (f:fs) (Id.catPosAux (c:ps)))
<|>
do c <- equivKey
g <- andOrFormula
return (AS_BASIC.Equivalence f g $ Id.tokPos c)
<|> return f
where makeImpl [f,g] p = AS_BASIC.Implication f g (Id.Range p)
makeImpl (f:r) (c:p) =
AS_BASIC.Implication f (makeImpl r p) (Id.Range [c])
makeImpl _ _ =
error "makeImpl got illegal argument"
-- | Parser for formulae with parentheses
parenFormula :: AnnoState.AParser st AS_BASIC.FORMULA
parenFormula =
do Lexer.oParenT << AnnoState.addAnnos
f <- impFormula << AnnoState.addAnnos
Lexer.cParenT >> return f
-- | Toplevel parser for formulae
aFormula :: AnnoState.AParser st (AS_Anno.Annoted AS_BASIC.FORMULA)
aFormula = Lexer.bind AS_Anno.appendAnno (AnnoState.annoParser $ impFormula) AnnoState.lineAnnos