0N/ADescription : Abstract syntax for propositional logic extended with QBFs
0N/ACopyright : (c) Jonathan von Schroeder, DFKI GmbH 2010
0N/AMaintainer : <jonathan.von_schroeder@dfki.de>
0N/AStability : experimental
0N/APortability : portable
0N/ADefinition of abstract syntax for propositional logic extended with QBFs
1472N/A ( FORMULA (..) -- datatype for Propositional Formulas
1472N/A , BASICITEMS (..) -- Items of a Basic Spec
1472N/A , BASICSPEC (..) -- Basic Spec
0N/A , SYMBITEMS (..) -- List of symbols
0N/A , SYMB (..) -- Symbols
0N/A , SYMBMAPITEMS (..) -- Symbol map
0N/A , SYMBORMAP (..) -- Symbol or symbol map
0N/A , PREDITEM (..) -- Predicates
3364N/A-- | predicates = propositions
0N/A-- | Datatype for QBF formulas
3393N/A -- pos: Propositional Identifiers
3364N/A ID t1 (Just t2) == ID t3 (Just t4) =
3364N/A || ((t2 == t3) && (t1 == t4))
3364N/A ID t1 Nothing == ID t2 t3 = (t1 == t2) || (Just t1 == t3)
3364N/A ID _ (Just _) == ID _ Nothing = False
0N/A-- two QBFs are equivalent if bound variables
0N/A-- can be renamed such that the QBFs are equal
2790N/AqbfMakeEqual (Just ids) f ts f1 ts1 = if length ts /= length ts1 then
0N/A (Predication t, Predication t1) -> if t == t1 then Just ids else
0N/A if t `elem` ts && t1 `elem` ts1 then
0N/A if ID t (Just t1) `elem` ids then
if ID t Nothing `notElem` ids && ID t1 Nothing `notElem` ids then
Just (ID t (Just t1) : ids)
(Negation f_ _, Negation f1_ _) -> qbfMakeEqual (Just ids) f_ ts f1_ ts1
(Conjunction (f_ : fs) _, Conjunction (f1_ : fs1) _) ->
if length fs /= length fs1 then Nothing else
(Conjunction fs nullRange) ts
(Conjunction fs1 nullRange) ts1
r = qbfMakeEqual (Just ids) f_ ts f1_ ts1
(Disjunction fs r, Disjunction fs1 r1) -> qbfMakeEqual (Just ids)
(Conjunction fs r) ts (Conjunction fs1 r1) ts1
(Implication f_ f1_ _, Implication f2 f3 _) -> case r of
_ -> qbfMakeEqual r f1_ ts f3 ts1
r = qbfMakeEqual (Just ids) f_ ts f2 ts1
(Equivalence f_ f1_ r1, Equivalence f2 f3 _) -> qbfMakeEqual (Just ids)
(Implication f_ f1_ r1) ts
(Implication f2 f3 r1) ts1
(ForAll ts_ f_ _, ForAll ts1_ f1_ _) -> case r of
(Just ids_) -> Just (ids ++ filter (\ (ID x (Just y)) ->
(x `elem` ts_ && y `notElem` ts1_) ||
(x `elem` ts1_ && y `notElem` ts_)) d)
r = qbfMakeEqual (Just ids) f_ (ts ++ ts_) f1_ (ts1 ++ ts1_)
(Exists ts_ f_ r, Exists ts1_ f1_ r1) -> qbfMakeEqual (Just ids)
qbfMakeEqual Nothing _ _ _ _ = Nothing
-- ranges are always equal (see
Common/Id.hs) - thus they can be ignored
instance Eq FORMULA where
FalseAtom _ == FalseAtom _ = True
TrueAtom _ == TrueAtom _ = True
Predication t == Predication t1 = t == t1
Negation f _ == Negation f1 _ = f == f1
Conjunction xs _ == Conjunction xs1 _ = xs == xs1
Disjunction xs _ == Disjunction xs1 _ = xs == xs1
Implication f f1 _ == Implication f2 f3 _ = (f == f2) && (f1 == f3)
Equivalence f f1 _ == Equivalence f2 f3 _ = (f == f2) && (f1 == f3)
ForAll ts f _ == ForAll ts1 f1 _ = isJust (qbfMakeEqual (Just []) f ts f1 ts1)
Exists ts f _ == Exists ts1 f1 _ = isJust (qbfMakeEqual (Just []) f ts f1 ts1)
data SYMBITEMS = SymbItems [SYMB]
Id.Range -- pos: SYMB_KIND, commas
data SYMBMAPITEMS = SymbMapItems [SYMBORMAP]
Id.Range -- pos: SYMB_KIND, commas
data SYMBORMAP = Symb SYMB
-- All about pretty printing
-- we chose the easy way here :)
instance Pretty FORMULA where
instance Pretty BASICSPEC where
instance Pretty SYMB where
instance Pretty SYMBITEMS where
instance Pretty SYMBMAPITEMS where
pretty = printSymbMapItems
instance Pretty BASICITEMS where
instance Pretty SYMBORMAP where
instance Pretty PREDITEM where
isPrimForm :: FORMULA -> Bool
-- Pretty printing for formulas
printFormula :: FORMULA -> Doc
let ppf p f = (if p f then id else parens) $ printFormula f
isJunctForm f = case f of
Implication _ _ _ -> False
Equivalence _ _ _ -> False
FalseAtom _ -> text falseS
Predication x -> pretty x
Negation f _ -> notDoc <+> ppf isPrimForm f
Conjunction xs _ -> sepByArbitrary andDoc $ map (ppf isPrimForm) xs
Disjunction xs _ -> sepByArbitrary orDoc $ map (ppf isPrimForm) xs
Implication x y _ -> ppf isJunctForm x <+> implies <+> ppf isJunctForm y
Equivalence x y _ -> ppf isJunctForm x <+> equiv <+> ppf isJunctForm y
ForAll xs y _ -> forallDoc <+> sepByArbitrary comma (map pretty xs)
Exists xs y _ -> exists <+> sepByArbitrary comma (map pretty xs)
sepByArbitrary :: Doc -> [Doc] -> Doc
sepByArbitrary d = fsep . prepPunctuate (d <> space)
printPredItem :: PREDITEM -> Doc
printPredItem (PredItem xs _) = fsep $ map pretty xs
printBasicSpec :: BASICSPEC -> Doc
printBasicSpec (BasicSpec xs) = vcat $ map pretty xs
printBasicItems :: BASICITEMS -> Doc
printBasicItems (AxiomItems xs) = vcat $ map pretty xs
printBasicItems (PredDecl x) = pretty x
printSymbol :: SYMB -> Doc
printSymbol (SymbId sym) = pretty sym
printSymbItems :: SYMBITEMS -> Doc
printSymbItems (SymbItems xs _) = fsep $ map pretty xs
printSymbOrMap :: SYMBORMAP -> Doc
printSymbOrMap (Symb sym) = pretty sym
printSymbOrMap (SymbMap source dest _) =
pretty source <+> mapsto <+> pretty dest
printSymbMapItems :: SYMBMAPITEMS -> Doc
printSymbMapItems (SymbMapItems xs _) = fsep $ map pretty xs