WeberLogic 0.1.1 → 0.1.2
raw patch · 4 files changed
+101/−39 lines, 4 files
Files
- README.md +76/−25
- WeberLogic.cabal +1/−1
- WeberLogic.lhs +10/−10
- WeberLogic/Parser.lhs +14/−3
README.md view
@@ -1,20 +1,21 @@-HsSymMath+WeberLogic ========= -Interactive mathematical languages written in haskell--## Logic.hs ##+Logic interpreter and parsing library -Formal logic parser +## Install ## -### Command line ###+```sh+cabal update+cabal install WeberLogic+``` -Currently the command line will take a logical expression as an argument and print out its corresponding truth table.+## Interpreter ## ```-$ ./Logic+$ ./WeberLogic Enter Command-> TruthTable: a&b+c->~a&b+> truthTable: a&b+c->~a&b 'a' 'b' 'c' | (((a&b)+c)->(~a&b)) True True True | False True True False | False@@ -26,38 +27,88 @@ False False False | True Enter Command-> ToNand: a&b->c +> toNand: a&b->c (((((a|b)|(a|b))|((a|b)|(a|b)))|(((a|b)|(a|b))|((a|b)|(a|b))))|(c|c)) Enter Command-> ToNor: a&b->c+> toNor: a&b->c (((((a/a)/(b/b))/((a/a)/(b/b)))/c)/((((a/a)/(b/b))/((a/a)/(b/b)))/c)) ``` -### Code ###+## Library ## +The library contains two modules. ++1. `WeberLogic.Parser`+2. `WeberLogic.Actions`++### WeberLogic.Parser ###++The `WeberLogic.Parser` provides functions which read stings and return+an abstract syntax tree (AST). The AST in implement with a data type+called `LogicExp` and `Letter`.++* Data Types+ * `LogicExp` - A recursively defined data type which implements as+ abstract syntax tree. It provides the following type constructors+ which function as nodes in the AST: `Not`, `And`, `Or`, `Implies`,+ `Iff`, `Nand`, and `Nor`. The `Predicate` type constructor+ functions as the AST leaves.+ + ```haskell+ > import WeberLogic.Parser+ > Predicate 'A' [(Variable 'x', Name 'a')]+ > Not (Predicate 'A' [(Variable 'x', Name 'a')])+ > And (Predicate 'A' [(Variable 'x', Name 'a')]) (Predicate 'B' [])+ ```++ * `Letter` - This Data Constructor provies two Type constructors+ `Variable` and `Name`. They are used in the construction of+ `Predicate` which requires a list of type `Letter`++ ```haskell+ > import WeberLogic.Parser+ > Predicate 'A' [(Variable 'x', Name 'a')]+ ```++* Functions+ * `parseExp`++ ```haskell+ > import WeberLogic.Parser+ > a = parseExp "Axa"+ > b = parseExp "~Axa"+ > c = parseExp "Axa&B"+ ```++ * `parseArg`++ ```haskell+ > import WeberLogic.Parser+ > a = parseExp "|- Axa"+ > b = parseExp "~Axa, B |- Cax"+ > c = parseExp "Axa&B, B, C |- Ax->By"+ ```++### WeberLogic.Actions ###++The `WeberLogic.Actions` modules provides functions which manipulate the+`LogicExp` AST. + ```haskell-> And (Atom 'a') (Not (Atom 'b'))-(a&~b)+> import WeberLogic.Parser+> import WeberLogic.Actions -> truthTable $ And (Atom 'a') (Not (Atom 'b'))+> mapM_ putStrLn $ truthTableStr $ readExp "A&B" 'a' 'b' | (a&~b) True True | False True False | True False True | False False False | False -> toNand $ And (Atom 'a') (Not (Atom 'b'))+> toNand $ readExp "A&~B" ((a|(b|b))|(a|(b|b))) -> toNor $ And (Atom 'a') (Not (Atom 'b'))+> toNor $ readExp "A&~B" ((a/a)/((b/b)/(b/b)))--> let exp = And (Atom 'a') (Not (Atom 'b'))-> exp == toNand exp-True-> exp == toNor exp-True-> exp == toNor (toNand exp)-True ```
WeberLogic.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: WeberLogic-version: 0.1.1+version: 0.1.2 synopsis: Logic interpreter description: Logic interpreter homepage: https://github.com/cameronbwhite/WeberLogic
WeberLogic.lhs view
@@ -29,25 +29,25 @@ > readCommand :: String -> Command > readCommand input = case words input of > ("truthTable:":arguments) -> -> case parse parseExp "truthtable" $ unwords arguments of +> case readExp $ unwords arguments of > Right val -> TruthTable val-> Left val -> Error $ show val+> Left val -> Error val > ("truthTree:":arguments) ->-> case parse parseArg "truthTree" $ unwords arguments of +> case readArg $ unwords arguments of > Right val -> TruthTree val-> Left val -> Error $ show val+> Left val -> Error val > ("toNand:":arguments) ->-> case parse parseExp "toNand" $ unwords arguments of +> case readExp $ unwords arguments of > Right val -> ToNand val-> Left val -> Error $ show val+> Left val -> Error val > ("toNor:":arguments) ->-> case parse parseExp "toNor" $ unwords arguments of +> case readExp $ unwords arguments of > Right val -> ToNor val-> Left val -> Error $ show val+> Left val -> Error val > ("isArgConsistent:":arguments) ->-> case parse parseArg "isArgConsistent" $ unwords arguments of +> case readArg $ unwords arguments of > Right val -> IsArgConsistent val-> Left val -> Error $ show val+> Left val -> Error val > _ -> Error input > runCommand :: Command -> IO()
WeberLogic/Parser.lhs view
@@ -3,7 +3,7 @@ > LogicExp( > Not, Or, Xor, And, Implies, Iff, Nand, Nor, Predicate, Universal, > Existential),-> parseExp, parseArg) +> readExp, readArg) > where > import Prelude hiding (and, or)@@ -34,8 +34,19 @@ > | Universal Letter LogicExp > | Existential LogicExp -> parseExp :: Parser LogicExp-> parseExp = do x <- parseExp1+> readExp :: String -> Either String LogicExp+> readExp str = case parse parseExp0 "readExp" str of +> Right val -> Right val+> Left val -> Left $ show val++> readArg :: String -> Either String [LogicExp]+> readArg str = case parse parseArg "readArg" str of +> Right val -> Right val+> Left val -> Left $ show val++> parseExp0 :: Parser LogicExp+> parseExp0 = do +> x <- parseExp1 > eof > return x