-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAS_CommonLogic.der.hs
298 lines (239 loc) · 9.28 KB
/
AS_CommonLogic.der.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
{-# LANGUAGE TypeSynonymInstances #-}
{- |
Module : $Header$
Description : Abstract syntax for common logic
Copyright : (c) Karl Luc, DFKI Bremen 2010, Eugen Kuksa and Uni Bremen 2011
License : GPLv2 or higher, see LICENSE.txt
Maintainer : eugenk@informatik.uni-bremen.de
Stability : provisional
Portability : portable
Definition of abstract syntax for common logic
-}
{-
Ref.
ISO/IEC IS 24707:2007(E)
-}
module CommonLogic.AS_CommonLogic where
import Common.Id as Id
import Common.Doc
import Common.DocUtils
import Common.Keywords
import qualified Common.AS_Annotation as AS_Anno
import Data.Set (Set)
import qualified Data.Set as Set (empty)
-- DrIFT command
{-! global: GetRange !-}
newtype BASIC_SPEC = Basic_spec [AS_Anno.Annoted BASIC_ITEMS]
deriving (Show, Ord, Eq)
data BASIC_ITEMS = Axiom_items [AS_Anno.Annoted TEXT_META]
deriving (Show, Ord, Eq)
emptyTextMeta :: TEXT_META
emptyTextMeta = Text_meta { getText = Text [] nullRange
, metarelation = Set.empty
, discourseNames = Nothing }
data TEXT_META = Text_meta { getText :: TEXT
, metarelation :: Set METARELATION
, discourseNames :: Maybe (Set NAME)
} deriving (Show, Ord, Eq)
-- TODO: check static analysis and other features on discourse names, as soon as segregated dialects are supported
-- Common Logic Syntax
data TEXT = Text [PHRASE] Id.Range
| Named_text String TEXT Id.Range
deriving (Show, Ord, Eq)
data PHRASE = Module MODULE
| Sentence SENTENCE
| Importation IMPORTATION
| Comment_text COMMENT TEXT Id.Range
deriving (Show, Ord, Eq)
data COMMENT = Comment String Id.Range
deriving (Show, Ord, Eq)
data MODULE = Mod NAME TEXT Id.Range
| Mod_ex NAME [NAME] TEXT Id.Range
deriving (Show, Ord, Eq)
data IMPORTATION = Imp_name NAME
deriving (Show, Ord, Eq)
data SENTENCE = Quant_sent QUANT_SENT Id.Range
| Bool_sent BOOL_SENT Id.Range
| Atom_sent ATOM Id.Range
| Comment_sent COMMENT SENTENCE Id.Range
| Irregular_sent SENTENCE Id.Range
deriving (Show, Ord, Eq)
data QUANT_SENT = Universal [NAME_OR_SEQMARK] SENTENCE
| Existential [NAME_OR_SEQMARK] SENTENCE
deriving (Show, Ord, Eq)
data BOOL_SENT = Conjunction [SENTENCE]
| Disjunction [SENTENCE]
| Negation SENTENCE
| Implication SENTENCE SENTENCE
| Biconditional SENTENCE SENTENCE
deriving (Show, Ord, Eq)
data ATOM = Equation TERM TERM
| Atom TERM [TERM_SEQ]
deriving (Show, Ord, Eq)
data TERM = Name_term NAME
| Funct_term TERM [TERM_SEQ] Id.Range
| Comment_term TERM COMMENT Id.Range
deriving (Show, Ord, Eq)
data TERM_SEQ = Term_seq TERM
| Seq_marks SEQ_MARK
deriving (Show, Ord, Eq)
type NAME = Id.Token
type SEQ_MARK = Id.Token
-- binding seq
data NAME_OR_SEQMARK = Name NAME
| SeqMark SEQ_MARK
deriving (Show, Eq, Ord)
data SYMB_MAP_ITEMS = Symb_map_items [SYMB_OR_MAP] Id.Range
deriving (Show, Ord, Eq)
data SYMB_OR_MAP = Symb NAME_OR_SEQMARK
| Symb_mapN NAME NAME Id.Range
| Symb_mapS SEQ_MARK SEQ_MARK Id.Range
deriving (Show, Ord, Eq)
data SYMB_ITEMS = Symb_items [NAME_OR_SEQMARK] Id.Range
-- pos: SYMB_KIND, commas
deriving (Show, Ord, Eq)
data METARELATION = RelativeInterprets NAME NAME [SYMB_MAP_ITEMS]
-- pos: delta, t2 - (this_text + delta entails t2)
| DefinablyInterprets NAME [SYMB_MAP_ITEMS]
-- pos: t2 - (this_text definably interprets t2)
| FaithfullyInterprets NAME NAME [SYMB_MAP_ITEMS]
-- pos: delta, t2 - (forall sigma. not (this_text + delta entails sigma) => not (t2 entails sigma))
| DefinablyEquivalent NAME [SYMB_MAP_ITEMS]
-- pos: t2 - (this_text ist definably equivalent to t2)
| NonconservativeExtends NAME [SYMB_MAP_ITEMS]
-- pos: t2 - (forall sigma. this_text entails sigma => t2 entails sigma)
| ConservativeExtends NAME [SYMB_MAP_ITEMS]
-- pos: t2 (this_text conservatively extends t2)
| IncludeLibs [NAME]
-- include the Libraries in the DevGraph
deriving (Show, Ord, Eq)
-- pretty printing using CLIF
instance Pretty BASIC_SPEC where
pretty = printBasicSpec
instance Pretty BASIC_ITEMS where
pretty = printBasicItems
instance Pretty TEXT_META where
pretty = printTextMeta
instance Pretty METARELATION where
pretty = printMetarelation
instance Pretty TEXT where
pretty = printText
instance Pretty PHRASE where
pretty = printPhrase
instance Pretty MODULE where
pretty = printModule
instance Pretty IMPORTATION where
pretty = printImportation
instance Pretty SENTENCE where
pretty = printSentence
instance Pretty BOOL_SENT where
pretty = printBoolSent
instance Pretty QUANT_SENT where
pretty = printQuantSent
instance Pretty ATOM where
pretty = printAtom
instance Pretty TERM where
pretty = printTerm
instance Pretty TERM_SEQ where
pretty = printTermSeq
instance Pretty COMMENT where
pretty = printComment
instance Pretty NAME_OR_SEQMARK where
pretty = printNameOrSeqMark
instance Pretty SYMB_OR_MAP where
pretty = printSymbOrMap
instance Pretty SYMB_MAP_ITEMS where
pretty = printSymbMapItems
instance Pretty SYMB_ITEMS where
pretty = printSymbItems
printBasicSpec :: BASIC_SPEC -> Doc
printBasicSpec (Basic_spec xs) = vcat $ map pretty xs
printBasicItems :: BASIC_ITEMS -> Doc
printBasicItems (Axiom_items xs) = vcat $ map pretty xs
printTextMeta :: TEXT_META -> Doc
printTextMeta tm = pretty $ getText tm
printMetarelation :: METARELATION -> Doc
printMetarelation _ = empty
printText :: TEXT -> Doc
printText s = case s of
Text x _ -> fsep $ map pretty x
Named_text x y _ -> parens $ text clTextS <+> text x <+> pretty y
printPhrase :: PHRASE -> Doc
printPhrase s = case s of
Module x -> parens $ text clModuleS <+> pretty x
Sentence x -> pretty x
Importation x -> parens $ text clImportS <+> pretty x
Comment_text x y _ -> parens $ text clCommentS <+> quotes (pretty x) <+> pretty y
printModule :: MODULE -> Doc
printModule (Mod x z _) = pretty x <+> pretty z
printModule (Mod_ex x y z _) =
pretty x <+> parens (text clExcludeS <+> fsep (map pretty y)) <+> pretty z
printImportation :: IMPORTATION -> Doc
printImportation (Imp_name x) = pretty x
printSentence :: SENTENCE -> Doc
printSentence s = case s of
Quant_sent xs _ -> parens $ pretty xs
Bool_sent xs _ -> parens $ pretty xs
Atom_sent xs _ -> pretty xs
Comment_sent x y _ -> parens $ text clCommentS <+> quotes (pretty x) <+> pretty y
Irregular_sent xs _ -> parens $ pretty xs
printComment :: COMMENT -> Doc
printComment s = case s of
Comment x _ -> text x
printQuantSent :: QUANT_SENT -> Doc
printQuantSent s = case s of
Universal x y -> text forallS <+> parens (sep $ map pretty x) <+> pretty y
Existential x y -> text existsS <+> parens (sep $ map pretty x) <+> pretty y
printBoolSent :: BOOL_SENT -> Doc
printBoolSent s = case s of
Conjunction xs -> text andS <+> (fsep $ map pretty xs)
Disjunction xs -> text orS <+> (fsep $ map pretty xs)
Negation xs -> text notS <+> pretty xs
Implication x y -> text ifS <+> pretty x <+> pretty y
Biconditional x y -> text iffS <+> pretty x <+> pretty y
printAtom :: ATOM -> Doc
printAtom s = case s of
Equation a b -> parens $ equals <+> pretty a <+> pretty b
Atom t ts -> parens $ pretty t <+> (sep $ map pretty ts)
printTerm :: TERM -> Doc
printTerm s = case s of
Name_term a -> pretty a
Funct_term t ts _ -> parens $ pretty t <+> (fsep $ map pretty ts)
Comment_term t c _ -> parens $ text clCommentS <+> quotes (pretty c) <+> pretty t
printTermSeq :: TERM_SEQ -> Doc
printTermSeq s = case s of
Term_seq t -> pretty t
Seq_marks m -> pretty m
-- Binding Seq
printNameOrSeqMark :: NAME_OR_SEQMARK -> Doc
printNameOrSeqMark s = case s of
Name x -> pretty x
SeqMark x -> pretty x
-- Alt x y -> pretty x <+> pretty y
printSymbOrMap :: SYMB_OR_MAP -> Doc
printSymbOrMap (Symb nos) = pretty nos
printSymbOrMap (Symb_mapN source dest _) =
pretty source <+> mapsto <+> pretty dest <> space
printSymbOrMap (Symb_mapS source dest _) =
pretty source <+> mapsto <+> pretty dest <> space -- space needed. without space the comma (from printSymbMapItems) would be part of the name @dest@
printSymbMapItems :: SYMB_MAP_ITEMS -> Doc
printSymbMapItems (Symb_map_items xs _) = ppWithCommas xs
printSymbItems :: SYMB_ITEMS -> Doc
printSymbItems (Symb_items xs _) = fsep $ map pretty xs
-- keywords, reservednames in CLIF
seqmarkS :: String
seqmarkS = "..."
orS :: String
orS = "or"
iffS :: String
iffS = "iff"
clCommentS :: String
clCommentS = "cl-comment"
clTextS :: String
clTextS = "cl-text"
clImportS :: String
clImportS = "cl-imports"
clModuleS :: String
clModuleS = "cl-module"
clExcludeS :: String
clExcludeS = "cl-excludes"