diff options
author | Camil Staps | 2015-07-03 13:49:53 +0200 |
---|---|---|
committer | Camil Staps | 2015-07-03 13:49:53 +0200 |
commit | 132b9b61ec36ff4ee2a16d7f42d7994850dc2de1 (patch) | |
tree | f9acf0c1c18f2213cd41f2a837b5ea1634bc34bd | |
parent | Cleanup (diff) |
Cleanup; simple truthtable; license; readme
-rw-r--r-- | Logic.dcl | 25 | ||||
-rw-r--r-- | Logic.icl | 28 | ||||
-rw-r--r-- | LogicTest.icl | 25 | ||||
-rw-r--r-- | README.md | 102 | ||||
-rw-r--r-- | StringUtils.dcl | 23 | ||||
-rw-r--r-- | StringUtils.icl | 23 |
6 files changed, 223 insertions, 3 deletions
@@ -1,3 +1,26 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ definition module Logic import StdEnv @@ -60,5 +83,7 @@ eval :: Expr -> [Bool] // Evaluate to a simpler form subexprs :: Expr -> [Expr] // Subexpressions of an expression sorted_subexprs :: (Expr -> [Expr]) // Similar, but roughly sorted by complexity + +simple_truthtable :: Expr -> TruthTable // Simple truthtable: only the atomic expression and the expression itself truthtable :: Expr -> TruthTable // Truthtable from an expression @@ -1,3 +1,26 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ implementation module Logic import StdEnv, StringUtils @@ -90,7 +113,7 @@ where line_b = "-" line_e = "-\n" line_s = "-+-" - padlens = map ((max 5) o strlen o toString) exprs + padlens = map ((max 5) o strlen o toString) exprs // 5 is the length of False toStringOrEmpty :: [Bool] -> String toStringOrEmpty [] = "" toStringOrEmpty [b:bs] = toString b @@ -231,6 +254,9 @@ subexprs (App2 e1 op e2) = removeDup (subexprs e1 ++ subexprs e2 ++ [e1,e2]) sorted_subexprs :: (Expr -> [Expr]) sorted_subexprs = sort o subexprs +simple_truthtable :: Expr -> TruthTable +simple_truthtable e = {exprs = [Atom a \\ a <- all_atoms e] ++ [e], options = all_atom_options e} + truthtable :: Expr -> TruthTable truthtable e = {exprs = sorted_subexprs e ++ [e], options = all_atom_options e} diff --git a/LogicTest.icl b/LogicTest.icl index abf3bd5..11559e7 100644 --- a/LogicTest.icl +++ b/LogicTest.icl @@ -1,3 +1,26 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ module LogicTest import Logic, StringUtils @@ -32,5 +55,5 @@ exprs_assoc = [e10,e11,e12,e13,e14,e15,e16,e17] //Start = map toString exprs_rand //Start = map toString exprs_assoc -Start = toString (truthtable (App2 e9 And e7)) +Start = toString (truthtable e15) @@ -1,6 +1,106 @@ # CleanLogic -Logic toolbox in [Clean](http://wiki.clean.cs.ru.nl/Clean) +Logic toolbox in [Clean](http://wiki.clean.cs.ru.nl/Clean). Features include: + + * Straightforward types for (atomic) expressions, operators and truth tables + * `toString` instances for all types that take care of associativity + * Substituting atomic expressions for constants + * Evaluating expressions without atomic expressions + * Generating truth tables for expressions, possibly with intermediate steps + * Displaying truth tables + +Read further for examples. + +## Types + +### Operators + +There is one unary operator (`Op1`): `Not`. + +There are four binary operators (`Op2`): `And`, `Or`, `Impl` and `Equiv`. `And` is considered left-associative; all others are considered right-associative. Associativity is only important in `toString` functions. `Expr` itself is an unambiguous recursive type. + +### Expressions + +Expressions are constants (`B Bool`), atomic expressions (`Atom Char`), or applications of operators on other expressions (`App1 Op1 Expr` or `App2 Expr Op2 Expr`). + +### Examples + + // 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')) + + // 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')) + +## Substitution + +You can substitute all occurences of `Atom a` in an `Expr e` by a `Bool b` with `substitute (a,b) e`. This yields a new `Expr`, in which all occurences have been replaced by the contstant `B b`. + +An alternative is `substitute_all`, which takes a list of tuples `(a :: AtomName, b :: Bool)` and substitutes all atomnames with the corresponding booleans. + +## Evaluation + +An expression `e` **without atomic expressions** may be evaluated to its result by calling `eval e`. This yields the empty list `[]` if `e` *did* have atomic expressions, or a singleton `[b :: Bool]` with the result of the evaluation. + +## Truth tables + +A `TruthTable` has a list of expressions `exprs` and a list of `options`, which has the type `[[(AtomName,Bool)]]` and describes all the options the truth table should list. + +A simple truth table of an expression `e` contains only the atomic expressions in `e` and `e` itself. It may be built with `simple_truthtable e`. A more complex truth table lists all `e`'s subexpressions. In either table, the expressions are roughly sorted by complexity. + +A truth table `table` can be shown with `toString table`. + +### Examples + +The `simple_truthtable` of `e10`: + + p | q | r | p & q & r + -------+-------+-------+----------- + False | False | False | False + False | False | True | False + False | True | False | False + False | True | True | False + True | False | False | False + True | False | True | False + True | True | False | False + True | True | True | True + +The `truthtable` of `e15`: + + p | q | r | q -> r | p -> q -> r + -------+-------+-------+--------+------------- + False | False | False | True | True + False | False | True | True | True + False | True | False | False | True + False | True | True | True | True + True | False | False | True | True + True | False | True | True | True + True | True | False | False | False + True | True | True | True | True + +## Future ideas + + * Different `toString` formats for operators: HTML (`¬`), UTF-8 (¬) + * Different `toString` formats for booleans: `0` and `1` or `T` and `F` instead of `True` and `False` + * Simplifying expressions with atomic expressions as far as possible + * Testing equivalence or impliance of expressions with atomic expressions ## License diff --git a/StringUtils.dcl b/StringUtils.dcl index b473246..e7255fb 100644 --- a/StringUtils.dcl +++ b/StringUtils.dcl @@ -1,3 +1,26 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ definition module StringUtils import StdEnv diff --git a/StringUtils.icl b/StringUtils.icl index 1c57a69..e2b7c3e 100644 --- a/StringUtils.icl +++ b/StringUtils.icl @@ -1,3 +1,26 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2015 Camil Staps + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ implementation module StringUtils import StdEnv |