-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathAS_BASIC_Propositional.der.hs
92 lines (80 loc) · 2.89 KB
/
AS_BASIC_Propositional.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
{-# 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
Definition of abstract syntax for propositional logic
-}
{-
Ref.
http://en.wikipedia.org/wiki/Propositional_logic
-}
module Propositional.AS_BASIC_Propositional
(
Formula (..) -- datatype for Propositional Formulas
, pretty -- pretty printing
, is_True_atom -- True?
, is_False_atom -- False?
) where
import Common.Id
import Common.Doc
import Common.DocUtils
-- DrIFT command
{-! global: UpPos !-}
-- | Datatype for propositional formulas
data Formula = Negation Formula Range
-- pos: not
| Conjunction [Formula] Range
-- pos: "/\"s
| Disjunction [Formula] Range
-- pos: "\/"s
| Implication Formula Formula Bool Range
-- pos: "=>"
| Equivalence Formula Formula Bool Range
-- pos: "<=>"
| True_atom Range
-- pos: "True"
| False_atom Range
-- pos: "False
| Predication Common.Id.Id
-- pos: Propositional Identifiers
deriving (Show, Eq, Ord)
instance Pretty Formula where
pretty = printFormula
-- | Value of the true atom
-- True is always true -P
is_True_atom :: Formula -> Bool
is_True_atom (True_atom _) = True
is_True_atom _ = False
-- | Value of the false atom
-- and False if always false
is_False_atom :: Formula -> Bool
is_False_atom (False_atom _) = False
is_False_atom _ = False
-- Pretty printing for formulas
printFormula :: Formula -> Doc
printFormula (Negation f _) = notDoc
<> lparen <> printFormula f <> rparen
printFormula (Conjunction xs _) = parens $
sepByArbitrary andDoc
$ map printFormula xs
printFormula (Disjunction xs _) = parens $
sepByArbitrary orDoc
$ map printFormula xs
printFormula (Implication x y _ _) = parens $ printFormula x <>
implies <> printFormula y
printFormula (Equivalence x y _ _) = parens $ printFormula x <>
equiv <> printFormula y
printFormula (True_atom _) = text "True"
printFormula (False_atom _) = text "False"
printFormula (Predication x) = idDoc x
-- Extended version of vcat
sepByArbitrary :: Doc -> [Doc] -> Doc
sepByArbitrary _ [] = text ""
sepByArbitrary _ (x:[]) = x
sepByArbitrary separator (x:xs) = x <> separator
<> (sepByArbitrary separator xs)