aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2015-07-03 11:16:02 +0200
committerCamil Staps2015-07-03 11:16:02 +0200
commit02c45922fc897047ecce16cc02398338947e7b94 (patch)
tree4e7af3276d29340139a561367363920104ff0c91
parentTypes, toString (diff)
Fix associativity
-rw-r--r--Logic.dcl2
-rw-r--r--Logic.icl12
-rw-r--r--LogicTest.icl33
3 files changed, 40 insertions, 7 deletions
diff --git a/Logic.dcl b/Logic.dcl
index 767990f..46228dc 100644
--- a/Logic.dcl
+++ b/Logic.dcl
@@ -36,3 +36,5 @@ instance toString Op1
instance toString Op2
instance toString Expr
+binds_stronger :: Op2 Op2 -> Bool // True iff arg1 stronger binds than arg2
+
diff --git a/Logic.icl b/Logic.icl
index 0276305..5d5fc26 100644
--- a/Logic.icl
+++ b/Logic.icl
@@ -85,17 +85,15 @@ needs_parentheses_right :: Expr -> Bool
needs_parentheses_right (App2 _ _ (B _)) = False
needs_parentheses_right (App2 _ _ (Atom _)) = False
needs_parentheses_right (App2 _ _ (App1 Not _)) = False
-needs_parentheses_right (App2 _ op1 (App2 _ op2 _)) = binds_stronger op1 op2
+needs_parentheses_right (App2 _ op1 (App2 _ op2 _)) = not (binds_stronger op2 op1)
// Associativity rules
binds_stronger :: Op2 Op2 -> Bool
-binds_stronger And _ = True // And is left-associative
-binds_stronger _ And = False
-binds_stronger Or Or = False // All others are right-associative
-binds_stronger Or _ = True
+binds_stronger _ And = False // And is left-associative
+binds_stronger And _ = True
+binds_stronger Or _ = True // The rest is right-associative
binds_stronger _ Or = False
-binds_stronger Impl Impl = False
binds_stronger Impl _ = True
binds_stronger _ Impl = False
-binds_stronger Equiv Equiv = False
+binds_stronger Equiv Equiv = True
diff --git a/LogicTest.icl b/LogicTest.icl
new file mode 100644
index 0000000..69adc8a
--- /dev/null
+++ b/LogicTest.icl
@@ -0,0 +1,33 @@
+module LogicTest
+
+import Logic
+
+// Some random examples
+e1 = Atom 'p'
+e2 = Atom 'q'
+e3 = App1 Not e1
+e4 = App2 e1 And e2
+e5 = App2 e3 Or e2
+e6 = App2 e3 Impl e3
+e7 = App2 e4 Equiv e5
+e8 = App2 (App2 (Atom 'p') And (Atom 'q')) Impl (Atom 'q')
+e9 = App2 (Atom 'p') And (App2 (Atom 'q') Impl (Atom 'q'))
+
+exprs_rand = [e1,e2,e3,e4,e5,e6,e7,e8,e9]
+
+// To test associativity rules
+e10 = App2 (App2 (Atom 'p') And (Atom 'q')) And (Atom 'r')
+e11 = App2 (Atom 'p') And (App2 (Atom 'q') And (Atom 'r'))
+
+e12 = App2 (App2 (Atom 'p') Or (Atom 'q')) Or (Atom 'r')
+e13 = App2 (Atom 'p') Or (App2 (Atom 'q') Or (Atom 'r'))
+
+e14 = App2 (App2 (Atom 'p') Impl (Atom 'q')) Impl (Atom 'r')
+e15 = App2 (Atom 'p') Impl (App2 (Atom 'q') Impl (Atom 'r'))
+
+e16 = App2 (App2 (Atom 'p') Equiv (Atom 'q')) Equiv (Atom 'r')
+e17 = App2 (Atom 'p') Equiv (App2 (Atom 'q') Equiv (Atom 'r'))
+
+exprs_assoc = [e10,e11,e12,e13,e14,e15,e16,e17]
+
+Start = map toString exprs_assoc